Java 未执行按方法的方面注释
我试图创建一个方面来监视某些方法的时间执行。我将注释定义为:Java 未执行按方法的方面注释,java,spring,annotations,aop,spring-aop,Java,Spring,Annotations,Aop,Spring Aop,我试图创建一个方面来监视某些方法的时间执行。我将注释定义为: @Retention(RetentionPolicy.RUNTIME) @Target( { ElementType.METHOD, ElementType.TYPE }) public @interface TimePerformance { } 这是方面代码: @Around("execution(* *(..)) && @annotation(timePerformance)") p
@Retention(RetentionPolicy.RUNTIME)
@Target(
{
ElementType.METHOD,
ElementType.TYPE
})
public @interface TimePerformance {
}
这是方面代码:
@Around("execution(* *(..)) && @annotation(timePerformance)")
public Object timePerformance(ProceedingJoinPoint pjp,TimePerformance timePerformance) throws Throwable {
if (LOG.isInfoEnabled()) {
LOG.info("AOP - Before executing "+pjp.getSignature());
}
Long startTime = System.currentTimeMillis();
Object result = pjp.proceed();
Long stopTime = System.currentTimeMillis();
LOG.info("MONITOR TIME_EXECUTION "+pjp.getSignature()+" : "+(stopTime-startTime));
if (LOG.isInfoEnabled()) {
LOG.info("AOP - After executing "+pjp.getSignature());
}
return result;
}
配置为:
<!-- AOP support -->
<bean id='stateAspectImpl' class='eu.genetwister.snpaware.ui.aspect.StateAspectImpl' />
<bean id='monitorImpl' class='eu.genetwister.snpaware.monitor.MonitorImpl' />
<aop:aspectj-autoproxy>
<aop:include name='stateAspectImpl' />
<aop:include name='monitorImpl' />
</aop:aspectj-autoproxy>
但是方面永远不会被执行,即使方法被执行
有人知道如何解决这个问题吗
谢谢为什么在切入点表达式中需要执行(..)? 我相信
@Around(value=@annotation()”)
应该可以工作。
因为,您是说,通过使用注释,需要检查使用此注释注释的任何方法,并调用@Around通知
在我看来,您不需要执行表达式,因为即使您的自定义注释可以应用于字段,SpringAOP也不支持字段方面
另外,是否需要在方面中绑定对象?您还可以从pjp.getArgs();
获取对象,以防万一
编辑:
这是一个方面
@Component(value = "regionAccessValidatorAspect")
@Aspect
public class RegionAccessValidatorAspect {
@Around(value = "@annotation(com.....RegionAccessValidator)")
public Object doAccessCheck(final ProceedingJoinPoint jp) throws Throwable {
。。。。。
}
这是我设置注释的处理器
@RegionAccessValidator(getVal = "Temporary")
public CountryProductOfferingRepresentation update(final CountryProductOfferingRepresentation cpoRep,
final RequestType requestType) throws Exception {
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RegionAccessValidator {
String getVal();
}
这是注释
@RegionAccessValidator(getVal = "Temporary")
public CountryProductOfferingRepresentation update(final CountryProductOfferingRepresentation cpoRep,
final RequestType requestType) throws Exception {
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RegionAccessValidator {
String getVal();
}
虽然我还没有通过传递参数来尝试,但今天我将尝试传递参数,并找出原因。为什么需要切入点表达式中的执行(..)?我相信@Around(value=@annotation())应该有效。使用@Around(“value=@annotation(.TimePerformance)”方法给出了一个关于格式错误的异常你能提到这个异常吗?上面说什么?这可能是因为方面不是组件或bean。这是异常:原因:java.lang.IllegalArgumentException:Pointcut格式不正确:字符位置6处应为“(”value=@annotation(eu.genetwsiter.snpaware.annotation.TimePerformance)具有
@TimePerformance
注释的bean是否在同一上下文中声明?正如我在评论中所说的,使用您的语法会导致来自at error的异常我不需要对象,但该对象需要避免由以下原因引起的异常:java.lang.IllegalArgumentException:error at::0找不到引用的切入点注释I just尝试了一个@Around建议,其设置与您的类似,对我来说效果非常好。但我在您的问题中注意到的是,切入点表达式中的(和*)之间没有空格。如果您使用的是