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 a JAR) and one is the actual application (which ends up as a WAR and depends on the JAR and imports the JAR's applicationContext.xml into it's own applicationContext.xml).
In the framework project, I've defined an aspect for all public methods.
@Aspect
@Configurable
public class MyAspect {
@Autowired
private SomeBean mBean;
@Pointcut("execution(public * *(..))")
public void anyPublicMethod() {
}
@Before("anyPublicMethod()")
public void checkAuthorization(JoinPoint pJoinPoint) {
mBean.doSomething();
}
}
And I've activated AOP in the applicationContext.xml of the framework (which is imported by the applicationContext.xml of the actual application project).
...
<context:spring-configured />
<context:component-scan base-package="com.mypackage" />
<aop:aspectj-autoproxy/>
...
When testing in the framework project, the aspect gets executed as expected when calling public methods on Spring beans.
As stated above, the framework project is included in the application project as a dependency but the aspect is not executed when calling matching methods (any public) in the application project on any Spring beans.
I've also tried using XML configuration of the aspect. That lead to the same behavior.
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).