Spring AOP:当配置位于 Servlet 上下文中而不是应用程序上下文中时触发方面?
我正在使用 Spring 3.0.x 和 Spring AOP。
所以,我有以下方面:
@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
}
}
我有以下类和方法:
public class Foo {
@Bar
public void doSomething() {
// do another thing
}
}
现在,这是我的应用程序上下文(没有我的 AOP 配置):
<bean id="testAspect" class="org.xyz.TestAspect" />
<bean id="foo" class="org.xyz.Foo" />
我正在尝试使用以下声明连接我的方面:
<aop:aspectj-autoproxy />
当我放置
在我的应用程序上下文中,切入点/方面没有被触发。 但是,如果我将
放入我的 servlet 配置中,一切都会很好,一切正常。
为什么我的上述设置可以在 servlet 上下文中使用
,但不能在应用程序上下文中使用???
编辑:
以下是相关的 web.xml 行:
<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>
<!-- 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>
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>
<!-- 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Servlet 上下文参数旨在由 ContextLoaderListener 读取(如果在您的 web.xml 中定义),而不是由 FrameworkServlet 读取(或其衍生产品,例如 DispatcherServlet)。
ContextLoaderListener 将从指定为 servlet 上下文参数的配置位置创建根应用程序上下文(所有 servlet 应用程序上下文的父级)。如果 servlet 也读取该内容,则将在 servlet 应用程序上下文中重新定义相同的 bean,而不是仅仅从父 app-ctx 继承它们。
The servlet context param is intended to be read by a
ContextLoaderListener
(if that is defined in yourweb.xml
), not by theFrameworkServlet
(or its derivatives, likeDispatcherServlet
).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.