我已經定義了以下攔截器:彈簧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());
}
執行下面的代碼段的方面方法被調用。要開始測試我的測試用例類看起來如下:
@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>
我絕對想不通,爲什麼在tomcat的部署時,AOP不起作用。我希望你有一些想法。
解決方案我找到了解決方案。我將我的aop配置放在applicationContext.xml中,但這不起作用。我將配置放在application-servlet.xml中,現在一切正常。有人知道爲什麼嗎?
如果我改變我的方法簽名到'public Object processAround(ProceedingJoinPoint pjp,OpenSession openSession)throws Throwable',我得到異常:「引發:java.lang.IllegalArgumentException:錯誤:: 0正式未綁定的切入點」 。 – Erik 2011-04-05 11:47:19
@Erik - 但也改變'@annotation(foo)' - 所以沒有FQN那裏,只是參數名稱。 – Bozho 2011-04-05 11:49:49
好的,現在程序編譯,但仍然沒有調用該方面。是否有某種記錄/調試可以用來查看發生了什麼? – Erik 2011-04-05 12:05:22