Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java springaop@annotation_Java_Spring_Aop - Fatal编程技术网

Java springaop@annotation

Java springaop@annotation,java,spring,aop,Java,Spring,Aop,我是AOP的新手,尤其是在春季AOP 我想用某种特定的方法记录执行时间。我阅读了Spring文档 并认为最好的解决方案是创建带有注释切入点的方面 它看起来像: @Around("@annotation(com.x.y.MethodExecutionTime)") public Object methodExecutionTimeLog(ProceedingJoinPoint joinPoint) throws Throwable StopWatch stopWatch = new St

我是AOP的新手,尤其是在春季AOP

我想用某种特定的方法记录执行时间。我阅读了Spring文档 并认为最好的解决方案是创建带有注释切入点的方面

它看起来像:

@Around("@annotation(com.x.y.MethodExecutionTime)")
public Object methodExecutionTimeLog(ProceedingJoinPoint joinPoint) throws Throwable 
    StopWatch stopWatch = new StopWatch();

    Object retVal = null;
    try {
        stopWatch.start();
        retVal = joinPoint.proceed();
        stopWatch.stop();
        logger.info("Execution time: " + stopWatch.getTotalTimeMillis() + " ms");
    } catch(Throwable e) {
        logger.error("Execution time: " + stopWatch.getTotalTimeMillis() + " ms");
        throw e;
    }
    return retVal;
}
注释用于方法:

    @Override
@MethodExecutionTime
public <T> T copy(Class<T> destType) {

    T t = ReflectionHelper.newInstance(destType);

    copyTo(t);

    return t;
}
@覆盖
@方法执行时间
公共T型副本(类别类型){
T=ReflectionHelper.newInstance(destType);
copyTo(t);
返回t;
}
Spring XML配置:

<context:spring-configured />
<aop:aspectj-autoproxy proxy-target-class="true" />

但它什么也不记录

我正在使用Spring3.0.5


有什么想法吗?谢谢

如果其他所有配置都正确,那么它应该是Spring AOP的限制之一(至少是默认配置的限制),即:

  • 要应用方面的对象应由Spring管理(即,它应该从应用程序上下文中获取,而不是使用
    new
    创建)

  • 调用应该起源于该对象的“外部”,即当您调用同一对象的另一个方法时,不会应用方面

  • 应该在与要应用方面的对象相同的应用程序上下文中声明(特别是在典型的SpringWebMVC应用程序中,
    applicationContext.xml
    …-servlet.xml
    形成不同的应用程序上下文)


您可以发布使用此注释的代码吗?您是否可以发布spring确认,该确认完成了使用spring AOP的设置?您是否在spring xml中启用了AOP?显示与aop相关的部分xml spring配置。我发布了它,是否有必要向spring xml配置文件添加一些其他选项?是否是使用
@Aspect
注释了
@Around
方法的类?它是Spring应用程序上下文的一部分吗?
MethodExecutionTime
Annotation的保留类型是什么?是运行时吗?嗨,axtavt,你能看看我在SpringAOP上的帖子吗-