Spring AOP 不适用于 Tomcat 和 tcserver
当我从单元测试或通过独立应用程序运行它时,我有一个方面工作得很好。但是,当我将它作为 Web 应用程序的一部分运行并将其托管在 Tomcat 上时,不会应用方面。
我的侧面看起来像
public class MyAspect {
@Around("within(com.service..*)")
public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
//do something
Object obj = pjp.proceed();
//do something else
return obj;
}
}
I have an aspect which works fine when I run it from a unit test or through a stand alone application. However when I run it as a part of web application and host it on Tomcat the Aspects are not applied.
My aspect looks like
public class MyAspect {
@Around("within(com.service..*)")
public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
//do something
Object obj = pjp.proceed();
//do something else
return obj;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够解决这个问题。原因是该方面是由 Web 应用程序上下文而不是全局应用程序上下文处理的,因此我必须重新构建一些内容。我在此处详细介绍了步骤
@ Seanizer Spring 确实支持。确实,它仅适用于方法,并且在其内部将适用于 com.service 的所有包和子包的方法。有关详细信息,请查看参考文档 这里
I am able to solve this. The reason was that the aspect were getting processed by web application context and not by global application context so I have to restructure couple of things. I have detailed the steps here
@seanizer Spring does support within. It's true that it is only applied to methods and in within it will apply to methods of all the package and sub package of com.service. For details check the reference documentation here
更新:我会将其保留,因为它仍然部分有效,即使它对您的情况没有帮助。不过,我会编辑一些地方,编辑标记为
this或 this。如果您使用 Spring AOP,则它无法工作。Spring AOP 仅完全支持执行
切入点。within
切入点仅在应用于方法执行时才有效,要获得within
的完整功能,您将需要 AspectJ (Spring AOP 仅使用一些 AspectJ 切入点,但不使用 AspectJ 编织器)。通过静态编译(通常通过 Maven 或 Ant) 或通过 加载时编织。此外,您的类缺少
@Aspect
注释。Update: I'll leave this in, because it's still partially valid, even if it didn't help in your case. I'll edit a few places though, edits are marked like
thisor this.If you're using Spring AOP, it can't work.Spring AOP only fully supports theexecution
pointcut. Thewithin
pointcut only works when it applies to method executions, for the full functionality ofwithin
you will need AspectJ (Spring AOP only uses some AspectJ pointcuts, but not the AspectJ weaver). Either through static compilation (usually through Maven or Ant) or through Load-Time-Weaving.Also, your class is missing an
@Aspect
annotation.怎么样?
迁移到 servlet-mvc.xml
How about move
to servlet-mvc.xml?