Spring Aspect not executed when defined in other JAR

后端 未结 4 2092
故里飘歌
故里飘歌 2021-01-18 08:27

I have a project consisting of two subprojects which are both Spring projects and have an applicationContext.xml each.

One is a framework project (which ends up as

相关标签:
4条回答
  • 2021-01-18 08:36

    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.

    0 讨论(0)
  • 2021-01-18 08:38

    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.

    0 讨论(0)
  • 2021-01-18 08:48

    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).

    0 讨论(0)
  • 2021-01-18 08:55

    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.

    0 讨论(0)
提交回复
热议问题