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>

 

自己记录下,有遇到类似问题的同学,给你参考下。

相关推荐