Tag: aop

Aspectj @Around切入Java中的所有方法

我正在编写一个简单的计时器方面来检测属于我的项目的所有包中的所有方法。 但是,那些类中的各种方法的返回类型是不同的,我得到以下错误 : 它只适用于setter但不适用于getter …… 错误:应用于不返回void的joinpoint 这是我的timeraspect … @Around(“execution(* com.myproject..*(..))”) public void log(ProceedingJoinPoint pjp) throws Throwable{ LOG.info(“TimerAspect”); String name = pjp.getSignature().getName(); Monitor mon = MonitorFactory.start(name); pjp.proceed(); mon.stop(); LOG.info(“TimerAspect Mon” + mon); String printStr = mon.getLabel()+”,”+mon.getUnits()+”,”+mon.getLastValue()+”,”+mon.getHits()+”,”+mon.getAvg()+”,”+mon.getTotal()+”,”+mon.getMin()+”,”+mon.getMax()+”,”+mon.getFirstAccess()+”,”+mon.getLastAccess(); File f = new File(“target/stats.csv”); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(f, true)); bufferedWriter.write(printStr); bufferedWriter.newLine(); bufferedWriter.flush(); bufferedWriter.close(); } 任何解决这个问题的线索都非常感谢。 谢谢

Struts 2 ModelDriven Action如何从beaing更新中排除某些属性

我们正在使用ModelDriven接口的动作。 该操作具有scopedModelDriven拦截器,我们将模型保存在会话中。 模型如下: public class Fundtransfer{ private String amount; private String sourceAccount; private String destinationAccount; private String sign //setter’s and getter’s ommited } Fundtransfer bean用作模型。 JSP表单有amount, sourceAcount和destinationAcount字段,它们通过modelDriven拦截器自动映射到Fundtransfer bean。 听到之前一切都很正常! 当然,用户可以简单地践踏HTML表单并将一个名为sign的变量传递给action,拦截器会将其更新为bean。 是否有可能我们可以通过ModelDriven拦截器无法更新符号值。 我知道我可以创建一个新bean,然后将其复制到Fundtransfer bean或使用某种inheritance。 有没有更好的方法。 我们使用的真正的bean是巨大的,还有一些嵌套的bean。 我想可能是我可以使用一些方面的技巧,但我不知道是否可能?

AspectJ建议不会在Maven多模块设置中触发

我正在尝试使用Aspectj进行AOP,但我不知道为什么不执行我的方面,它只运行主类。 这是我第一次这样做,所以我可能做错了什么。 这是我的代码: 方面: @Aspect public class YourAspect { @Pointcut(“@annotation(yourAnnotationVariableName)”) public void annotationPointCutDefinition(YourAnnotation yourAnnotationVariableName){ } @Pointcut(“execution(* *(..))”) public void atExecution(){} @Around(“annotationPointCutDefinition(yourAnnotationVariableName) && atExecution()”) public Object aroundAdvice(ProceedingJoinPoint joinPoint, YourAnnotation yourAnnotationVariableName) throws Throwable { if(yourAnnotationVariableName.isRun()) { Object returnObject = null; try { System.out.println(“aspects.YourAspect’s aroundAdvice’s body is now executed Before yourMethodAround is called.”); returnObject = joinPoint.proceed(); } catch […]

用于拦截所有exception的Spring AOP配置

我正在努力编写/配置一个ThrowsAdvice拦截器,我想拦截整个项目中抛出的所有exception: public class ExceptionsInterceptor implements ThrowsAdvice { public void afterThrowing(final Method p_oMethod, final Object[] p_oArgArray, final Object p_oTarget, final Exception p_oException) { System.out.println(“Exception caught by Spring AOP!”); } } 我已经成功配置了一个MethodInterceptor实现,它拦截了我想要分析的特定方法(看看它们执行需要多长时间)。 这是我到目前为止的XML配置文件: 我的ProfilingInterceptor工作得很完美,并且当我的Main :: doSomething()方法被调用时精确拦截 – 所以我知道我是ontrack。 使用XmlSpy来查看Spring AOP的模式,看起来我可以添加类似下面的内容,以便让我的ExceptionsInterceptor拦截所有抛出的exception: 但是我找不到任何以此为例的文档,我不知道如何配置方法属性,使其成为“通配符”(*)并匹配所有类和所有方法。 谁能指出我正确的方向? 提前致谢!

无法重新定义类当我尝试重新转换类时

我试图动态修改类,例如在行之前调用sleep()。 我在运行时使用Attach方法Attach代理程序Attach到jvm。 然后我从jvm获得了目标类,并对其进行了修改(添加一行来调用sleep() )。 我得到了redine类错误。 我使用的是JDK1.6。 我正在使用ASM核心API来修改类。 错误: Caused by: java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields) at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method) at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:124) ASM代码有问题吗? 实际上我的ASM代码完成了它的工作(添加一行来调用sleep() )。 当前的jvm不支持转换类吗? 似乎无法执行retransformClasses() 。 retransformClasses()不支持ASM操作(在方法中添加一行来调用sleep() )? 有任何想法吗? 谢谢 编辑:我要修改的类: import java.util.concurrent.TimeUnit; public class Person { public String name = “abc”; public String address = “xxxxx” ; public […]

Spring @Before更改每个用户登录

我有一个@Aspect类,它执行@Before方法并使用用户登录中的一些Id拦截所有查询。 但问题是:每次用户登录时,@Before方法的Id都会更改为所有已登录用户,只需更改当前用户即可。 简单的过程:当用户登录时,@ Aspect类获取其id以拦截所有查询。 但是这个id正在为每个登录用户而改变。 也许应该是会话的问题,我真的不知道。 方面类: @Aspect @Component @Transactional(propagation = Propagation.REQUIRED) public class TenancyAspect { @Autowired private EntityManager manager; @Autowired private AppUserDetailsService appUserDetailsService; @Before(“execution(* com.tc.tcqualidade.repository.*.*(..)) ” +”&& !execution(* com.tc.tcqualidade.repository.UsuarioRepository.porEmailEStatus(..))” +”&& !execution(* com.tc.tcqualidade.repository.UsuarioRepository.permissoes(..))”) public void definirTenant() { String tenantid = appUserDetailsService.getTenantId(); if (tenantid != null) { manager.unwrap(Session.class).enableFilter(“tenant”).setParameter(“id”, tenantid); } } } 登录类: @Service public class […]

Spring Transaction Management:使用@Transactional与使用AOP(<aop:advisor)

我对Spring事务管理感到困惑。 在我的应用程序中,我使用服务类中的@Transactional实现了事务管理。 我配置我的spring.xml就像: ${jdbc.dialect} false update 如果我在配置文件中实现事务管理,而不在服务类中使用@Transactional: 它是否比@Transactional给我任何好处? 有人告诉我使用@Transactional也是在spring实施AOP。 谁能解释我怎么样?

如何在aspectJ中排除getter和setter?

我在我的maven项目中有一个类aspectJ,它让我在项目中显示任何被调用方法的Begin和End。 我现在尝试排除所有的getter和setter。 我尝试修改这个注释: @Around(“execution(public * *(..)) by @Around(“execution(public * *(..) && !within(* set*(..))”) 但它没有whork,它给了我在consol: [ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.7:compile (default) on project spb-lceb: AJC compiler errors: [ERROR] error at @Around(“execution(public * *(..) && !within(* set*(..))”) Syntax error on token “execution(public * *(..) && !within(* set*(..))”, “)” expected 任何的想法

AOP使用Around来避免执行方法

我在我的代码中使用Spring AOP来拦截某个方法的执行。 我正在尝试做的一个简化示例如下: public void someMethod() { //does something } @Around(“execution( someMethod())”) public void anotherMethod(final ProceedingJoinPoint joinPoint) { //i want to add this to a queue to get executed later on addToWaitList(new Callable() { @Override public call() throws Exception { joinPoint.proceed(); } }); return; } 本质上,我想推迟someMethod()的执行,直到它位于列表的头部。 但是,主线程阻塞,即使我在anotherMethod()的末尾返回,所以我无法将new Callable添加到列表中,直到第一个完成执行。 文档说您可以通过返回自己的返回值或抛出exception来快速建议的方法执行。 我不想抛出exception而且我不确定在这种情况下“返回自己的返回值”意味着什么。 我希望能够使用主线程将Callables添加到列表中,然后让其他线程池执行它们。

无法使用Spring LoadTimeWeaving将建议应用于第三方库(log4g – Logger.getLogger调用)?

您好不能使用Spring LoadTimeWeaving向第三方库(log4g-Logger.getLogger调用)提出建议? 1. SpringConfig.xml <!– –> 2. META-INF目录中的aop.xml。 3. Aspect类 package com.app.svc; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class MyLoggingAspect { @Around(“getLoggerPointCut()”) public Object changeFileAppender(ProceedingJoinPoint pjp) throws Throwable { System.err.println(“—————- MyLoggingAspect ##——————–“); Object proceed = pjp.proceed(); return proceed; } //This is not working – Pointcuts for classes in 3rd […]