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

时间:2011-04-05 10:17:57

标签: java spring java-ee aspectj spring-aop

我定义了以下拦截器:

@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;
    }

}

当我在弹簧测试中执行以下代码时

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

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

@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中,现在一切都很好。有人知道为什么吗?

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:1)

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

我假设您正在使用<context:component-scan ...>标记,并将其放在servlet XML中。这就是为什么你需要在servlet XML中同时使用它们的原因,否则它们不会彼此“看到”。结果,连接没有正确建立。