Spring AOP - 切入点/拦截器未调用

发布于 2024-10-30 00:21:42 字数 1862 浏览 3 评论 0原文

我定义了以下拦截器:

@Aspect
public class OpenSessionInRequestInterceptor {

    private Log log = LogFactory.getLog(getClass());

    @Autowired
    private SessionFactory sessionFactory;

    public OpenSessionInRequestInterceptor() {

    }

    @Around("@annotation(com.sc2.master.aop.hibernate.OpenSession)")
    public Object processAround(ProceedingJoinPoint pjp) throws Throwable {
        log.info("Opening Hibernate Session in method "+pjp.getSignature());
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));

        Object ret = pjp.proceed();

        session.close();
        TransactionSynchronizationManager.unbindResource(sessionFactory);

        log.info("Closing Hibernate Session in method "+pjp.getSignature());

        return ret;
    }

}

当我在 spring 测试中执行以下代码时,

    @OpenSession
    public void call() {
        BusinessCustomer customer = (BusinessCustomer) this.customerDao.loadAll().get(0);
        System.out.println(customer.getContacts().size());
    }

将调用方面方法。要开始测试,我的测试用例类如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:WebContent/WEB-INF/applicationContext.xml"})
@Transactional

但是,当我有一个用 @OpenSession 注释的方法并将该应用程序部署在我的 Tomcat 服务器上时,不会调用拦截器方法。

应用程序上下文定义如下:

<aop:aspectj-autoproxy proxy-target-class="true">
</aop:aspectj-autoproxy>

<bean id="openSessionInRequestInterceptor" class="OpenSessionInRequestInterceptor"></bean>

我完全不明白,为什么AOP在部署在tomcat上时不起作用。我希望你有一些想法。

解决方案我找到了解决方案。我将 aop 配置放在 applicationContext.xml 中,但这不起作用。我将配置放在 application-servlet.xml 中,现在一切都很好。有人知道为什么吗?

I have defined the following interceptor:

@Aspect
public class OpenSessionInRequestInterceptor {

    private Log log = LogFactory.getLog(getClass());

    @Autowired
    private SessionFactory sessionFactory;

    public OpenSessionInRequestInterceptor() {

    }

    @Around("@annotation(com.sc2.master.aop.hibernate.OpenSession)")
    public Object processAround(ProceedingJoinPoint pjp) throws Throwable {
        log.info("Opening Hibernate Session in method "+pjp.getSignature());
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));

        Object ret = pjp.proceed();

        session.close();
        TransactionSynchronizationManager.unbindResource(sessionFactory);

        log.info("Closing Hibernate Session in method "+pjp.getSignature());

        return ret;
    }

}

When I execute the following piece of code in a spring test

    @OpenSession
    public void call() {
        BusinessCustomer customer = (BusinessCustomer) this.customerDao.loadAll().get(0);
        System.out.println(customer.getContacts().size());
    }

the aspect method is called. To start the test my test case class looks as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:WebContent/WEB-INF/applicationContext.xml"})
@Transactional

However, when I have a method annotated with @OpenSession and deploy the application on my Tomcat server, the interceptor method is not called.

The application context definition looks as follows:

<aop:aspectj-autoproxy proxy-target-class="true">
</aop:aspectj-autoproxy>

<bean id="openSessionInRequestInterceptor" class="OpenSessionInRequestInterceptor"></bean>

I can absolutely not figure out, why AOP does not work when deployed on the tomcat. I hope you have some ideas.

Solution I found the solution. I places my aop configuration in the applicationContext.xml, but this will not work. I placed the configuration in the application-servlet.xml and now everything is fine. Has someone an idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

左秋 2024-11-06 00:21:42

我承认我不必使用标记注释来使其工作,但我需要注释作为参数,所以这有效:

@Around("@annotation(foo)")
public Object invoke(ProceedingJoinPoint invocation, Foo foo) throws Throwable 

但是..请注意,如果没有,@Transactional也会启动会话还没有开始,所以也许你并不真正需要它。

更新:如果您的bean是在子上下文中定义的,那么父上下文的aop配置不会影响它们。父上下文看不到子上下文,并且您的 x-servlet.xml 是子上下文。

I admit I didn't have to make it work using a marker annotation, but I needed the annotation as argument, so this worked:

@Around("@annotation(foo)")
public Object invoke(ProceedingJoinPoint invocation, Foo foo) throws Throwable 

But.. note that @Transactional also starts a session if one isn't started, so perhaps you don't really need that.

Update: if your beans are defined in the child context, then the aop configuration of the parent context does not affect them. The parent context does not see the child context, and your x-servlet.xml is a child context.

疯狂的代价 2024-11-06 00:21:42

要回答为什么必须将配置放入 servlet XML 中才能开始工作:

我假设您正在使用 标记并且它被放置在 servlet XML 中。这就是为什么您需要将它们都放在 servlet XML 中,否则它们不会“看到”彼此。结果,无法正确建立连接。

To answer why you have to put configuration in the servlet XML to get to work:

I assume you are using <context:component-scan ...> tag and this is placed in the servlet XML. That is the reason why you need to have them both in servlet XML, otherwise they don't "see" each other. As a result, the connection is not properly established.

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