Spring AOP: Aspect triggering when configuration is in Servlet context, but not application context?
I'm using Spring 3.0.x with Spring AOP.
So, I have the following Aspect:
@Aspect
public class TestAspect {
@Pointcut(value="@annotation(Bar)", argNames="Bar")
public void pointCutMethod(Bar bar)
{
}
@Before(value="pointCutMethod(Bar)", argNames="Bar")
public void wrapPublishMethod(Bar bar) throws Throwable
{
// Do something crazy
}
}
And I have the following class and method:
public class Foo {
@Bar
public void doSomething() {
// do another thing
}
}
Now, here is my application context (without my AOP config):
<bean id="testAspect" class="org.xyz.TestAspect" />
<bean id="foo" class="org.xyz.Foo" />
I'm trying to wire up my aspect using the following declaration:
<aop:aspectj-autoproxy />
When I place <aop:aspectj-autoproxy />
in my application context, the pointcut/aspect is not getting triggered. However, if I place <aop:aspectj-autoproxy />
in my servlet configuration, all is well and everything works.
Why does my above setup work with <aop:aspectj-autoproxy />
in the servlet context, but not in the application context???
EDIT:
Here are the relevant web.xml lines:
<servlet>
<servlet-name>XYZ</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- Aspect works when config line is in this file -->
<param-value>/WEB-INF/classes/xyz-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
开发者_JAVA百科 <!-- Aspect DOES NOT work when config line is in one of the files below -->
<param-value>/WEB-INF/classes/xyz-application-context.xml, /WEB-INF/classes/xyz-aspectConfig.xml</param-value>
</context-param>
The servlet context param is intended to be read by a ContextLoaderListener
(if that is defined in your web.xml
), not by the FrameworkServlet
(or its derivatives, like DispatcherServlet
).
The ContextLoaderListener
would create a root application context (parent of all the servlet application contexts) from the config locations specified as servlet context parameter. If the servlets would read that too, the same beans would be re-defined within the servlet application context instead of just inheriting them from the parent app-ctx.
精彩评论