spring aop事務配置注意事項

業務邏輯模塊需要事務,在spring中是太常見不過的事情了。AOP配置事務,網上文章很多,大概就是這個樣子:

	<aop:config > 
		<!-- 
		<aop:pointcut id="allManagerMethod" expression="execution(* com.amarsoft..*(..))" /> 
		<aop:pointcut id="custService" expression="execution(* com.amarsoft.cust.service..*(..))" /> 
		 -->
		<aop:pointcut id="service" expression="execution(* com.amarsoft..*ServiceImpl.*(..))" /> 
		<aop:advisor advice-ref="txAdvice" pointcut-ref="service" /> 
	</aop:config> 

 

調用代碼:

	@Test
	public void doTransaction(){
		CustomerService custService = Apx.bean(EntCustomerServiceImpl.class);
		EntCustomer entCust = new EntCustomer();
		entCust.setCustName("測試-公司客戶1-客戶處理服務");
		entCust.setChnName("測試-公司客戶1-客戶處理服務");
		entCust.setCustType("10");
		entCust.setCertType("Ent01");
		entCust.setCertNo("71724724-6");
		int r = 0;
		try {
			r = custService.saveCustomer(entCust);
			Assert.assertEquals(1, r);
			r = custService.deleteCustomer(entCust.getCustId());
//			Assert.assertEquals(2, r);	//刪除CI表和EI表,因此這裏是2
		} catch (CustomerException e) {
			e.printStackTrace();
		}
		
		
	}

 然後異常出來了:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.amarsoft.cust.service.impl.EntCustomerServiceImpl] is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:295)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)
	at com.amarsoft.apm.ApmContext.getBean(ApmContext.java:124)
	at com.amarsoft.apm.Apx.bean(Apx.java:36)
	at com.amarsoft.cust.CustomerServiceTestCase.doTransaction(CustomerServiceTestCase.java:24)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

網上查了N多資料,最後一個東西引起了我的注意:

 

proxy-target-class="true"

於是調整了下配置文件 :

	<aop:config proxy-target-class="true"> 
		<!-- 
		<aop:pointcut id="allManagerMethod" expression="execution(* com.amarsoft..*(..))" /> 
		<aop:pointcut id="custService" expression="execution(* com.amarsoft.cust.service..*(..))" /> 
		 -->
		<aop:pointcut id="service" expression="execution(* com.amarsoft..*ServiceImpl.*(..))" /> 
		<aop:advisor advice-ref="txAdvice" pointcut-ref="service" /> 
	</aop:config> 

 

自己記錄下,有遇到類似問題的同學,給你參考下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章