Spring Aspect not executed when defined in other JAR

给你一囗甜甜゛ 提交于 2019-12-01 17:23:51

IMHO, you can tweak the approach slightly.

The first thing I would do is to delegate configuration of the application context for the war to the web.xml :

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/spring*.xml</param-value>
</context-param>

Secondly I would enable aop in you war file's application context, as this is where you want to use it. It sounds, at the moment, like you are importing the application context with aop configuration just to get it in your web project, which is maybe wrong.

Finally i'm making the assumption that these are runtime and not compiled aspects, in the latter case you would need to recompile with aspectj in your war project regardless of the dependency.

In Spring MVC webapps, you actually have 2 contexts: the root context and the servlet's context. Be sure to configure your aspect in the servlet's context. Indeed, the servlet's context "sees" the root one - and not the other way around:

In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.

So your import of the framework config has to be made in the [servlet-name]-servlet.xml file rather than in the other one(s) if you want your aspects to be applied to its beans.

That's what I do:

<context:annotation-config/>
<context:component-scan base-package="my.pkg" scoped-proxy="interfaces"/> 
<aop:aspectj-autoproxy proxy-target-class="true" />

Although I cannot tell if it will work in your case.... it would help if you set up a stripped down version of your project at github.

This does not require any byte-code weaving or instrumenting the jvm. Just make sure that you use auto-injected attributes when calling the method in question, ie.

@Autowired
private MyType myTypeInstance; // MyType is usually an interface

public void someMethod() {
    // myTypeInstance is actually a proxy object... thereby providing the
    // access point for the weaving stuff. (as far as I understand it)
    myTypeInstance.method();
}

Otherwise, follow these instructions if you don't like the spring-managed AOP class proxies.

I think you need to make your aspect a component to have it recognised by spring:

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-at-aspectj

Autodetecting aspects through component scanning

You may register aspect classes as regular beans in your Spring XML configuration, or autodetect them through classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate @Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring's component scanner).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!