Spring AOP 不适用于 Tomcat 和 tcserver

发布于 2024-09-30 21:59:04 字数 382 浏览 0 评论 0原文

当我从单元测试或通过独立应用程序运行它时,我有一个方面工作得很好。但是,当我将它作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

峩卟喜欢 2024-10-07 21:59:04

我能够解决这个问题。原因是该方面是由 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

夏雨凉 2024-10-07 21:59:04

更新:我会将其保留,因为它仍然部分有效,即使它对您的情况没有帮助。不过,我会编辑一些地方,编辑标记为 thisthis

如果您使用 Spring AOP,则它无法工作。 Spring AOP 仅完全支持执行切入点。 within 切入点仅在应用于方法执行时才有效要获得 within 的完整功能,您将需要 AspectJ (Spring AOP 仅使用一些 AspectJ 切入点,但不使用 AspectJ 编织器)。通过静态编译(通常通过 MavenAnt) 或通过 加载时编织

此外,您的类缺少 @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 this or this.

If you're using Spring AOP, it can't work. Spring AOP only fully supports the execution pointcut. The within pointcut only works when it applies to method executions, for the full functionality of within 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.

苍景流年 2024-10-07 21:59:04

怎么样?

<context:component-scan base-package="com.*" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />   

迁移到 servlet-mvc.xml

How about move

<context:component-scan base-package="com.*" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />   

to servlet-mvc.xml?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文