简体   繁体   English

方面永远不会被调用

[英]Aspect never gets called

My goal is to execute some code each time a method with a particular annotation completes its execution. 我的目标是每次带有特定批注的方法完成其执行时都执行一些代码。 I have the following: 我有以下几点:

@Aspect
public class MonitoringAspect {
    @After("@annotation(MonitorExecution)")
    public void onFinished(JoinPoint jp) {
        System.out.println("called!");
    }
}

The code of the MonitorExecution annotation is as follows: MonitorExecution批注的代码如下:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MonitorExecution {
}

This happens inside a Spring4 application and I have declared the MonitoringAspect as a bean like: 这是在Spring4应用程序内部发生的,并且我已将MonitoringAspect声明为像这样的bean:

<bean id="monitoringAspect" class="com.....MonitoringAspect" />
<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="monitoringAspect"/>
</aop:aspectj-autoproxy>

I have a public method inside a general-purpose class (ie not managed by spring / not a component) that is annotated with the @MonitorExecution annotation. 我在用@MonitorExecution批注进行批注的通用类(即不是由spring /而不是组件管理)内有一个公共方法。 I have successfully verified that the aforementioned method gets called, but the aspect is never triggered. 我已经成功验证了上述方法已被调用,但从未触发过该方面。 Any ideas what might be the issue? 任何想法可能是什么问题?

aspectj-autoproxy means that proxy classes will be created for each Spring managed bean (using JDK dynamic proxies or CGLIB) and using these proxies you are able to intercept methods invocation. aspectj-autoproxy意味着将为每个Spring托管bean创建代理类(使用JDK动态代理或CGLIB),使用这些代理,您可以拦截方法调用。 Thus, if your annotated method is method of a class outside Spring Context, aspect will not work 因此,如果您带注释的方法是Spring Context之外的类的方法,则方面将不起作用

Despite it if you still want to intercept it, you will have to use AspectJ only or in conjunction with Spring. 尽管如此,如果您仍然想拦截它,则必须只使用AspectJ或与Spring结合使用。 For second option you will have to enable load-time weaving in your spring configuration 对于第二个选项,您将必须在弹簧配置中启用加载时编织

Have a look documentation for details: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop 请查看文档以获取详细信息: https : //docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM