当前位置: 代码迷 >> ASP >> Spring2.X以AspectJ 式AOP 配备事务
  详细解决方案

Spring2.X以AspectJ 式AOP 配备事务

热度:720   发布时间:2012-10-19 16:53:35.0
Spring2.X以AspectJ 式AOP 配置事务
(1)配置:
??? Spring的事务管理是通过AOP代理实现的,其中的事务通知由元数据驱动。代理对象与事务元数据结合产生一个AOP代理,它使用一个PlatformTransactionManager实现,配合TransactionInterceptor,在方法调用前后实施事务。
Java代码 复制代码
  1. <?xml?version="1.0"?encoding="UTF-8"?> ??
  2. ??
  3. <beans?xmlns="http://www.springframework.org/schema/beans"? ??
  4. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ??
  5. ????xmlns:context="http://www.springframework.org/schema/context"? ??
  6. ????xmlns:aop="http://www.springframework.org/schema/aop"? ??
  7. ????xmlns:tx="http://www.springframework.org/schema/tx"? ??
  8. ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd? ??
  9. ????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-2.5.xsd? ??
  10. ????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd? ??
  11. ????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> ??
  12. ??
  13. ????<description>springApp</description>???? ??
  14. ????<!--?dataSource?for?MySQL?--> ??
  15. ????<bean?id="dataSource"??
  16. ????????class="org.apache.commons.dbcp.BasicDataSource"??
  17. ????????destroy-method="close"> ??
  18. ????????<property?name="driverClassName"??
  19. ????????????value="com.mysql.jdbc.Driver"?/> ??
  20. ????????<property?name="url"??
  21. ????????????value="jdbc:mysql://localhost:3306/springapp"?/> ??
  22. ????????<property?name="username"?value="root"?/> ??
  23. ????????<property?name="password"?value="****"?/> ??
  24. ????</bean>??? ??
  25. ??
  26. ????<!--?Hibernate?SessionFactory?for?MySQL?--> ??
  27. ????<bean?id="sessionFactory"??
  28. ????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> ??
  29. ????????<property?name="dataSource"?ref="dataSource"?/> ??
  30. ??
  31. ????????<property?name="mappingDirectoryLocations"> ??
  32. ????????????<list> ??
  33. ????????????????<value>classpath:/</value> ??
  34. ????????????</list> ??
  35. ????????</property> ??
  36. ????????<property?name="hibernateProperties"> ??
  37. ????????????<props> ??
  38. ????????????????<prop?key="hibernate.dialect"> ??
  39. ????????????????????org.hibernate.dialect.MySQLDialect ??
  40. ????????????????</prop> ??
  41. ????????????????<prop?key="hibernate.show_sql">true</prop> ??
  42. ????????????????<prop?key="hibernate.jdbc.fetch_size">50</prop> ??
  43. ????????????????<prop?key="hibernate.jdbc.batch_size">100</prop> ??
  44. ????????????</props> ??
  45. ????????</property> ??
  46. ??
  47. ????</bean> ??
  48. ??
  49. ??
  50. ????<!--Transaction?-->? ??
  51. ????<bean?id="txManager"? ??
  52. ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">? ??
  53. ????????????<property?name="sessionFactory"?ref="sessionFactory"?/>? ??
  54. ????</bean>? ??
  55. ??
  56. ????<aop:config>?<!--use?CGLIB:proxy-target-class="true--> ??
  57. ????????<aop:pointcut?id="serviceOperator"?expression="execution(*?com.logcd.business.service.*.*(..))"/> ??
  58. ????????<aop:advisor??advice-ref="txAdvice"?pointcut-ref="serviceOperator"/>? ??
  59. ????????<!-- ??
  60. ????????<aop:advisor?pointcut="execution(*?com.logcd.business.service..*Service.*(..))"?advice-ref="txAdvice"/>??????? ??
  61. ????????--> ??
  62. ????</aop:config>? ??
  63. ???? ??
  64. ????<tx:advice?id="txAdvice"?transaction-manager="txManager">? ??
  65. ????????<tx:attributes>? ??
  66. ????????????<tx:method?name="find*"?read-only="true"?/>? ??
  67. ????????????<tx:method?name="load*"?read-only="true"?/>? ??
  68. ????????????<tx:method?name="is*"?read-only="true"/>? ??
  69. ????????????<tx:method?name="save*"? ??
  70. ????????????????rollback-for="Exception"/>? ??
  71. ????????????<tx:method?name="insert*"? ??
  72. ????????????????rollback-for="Exception"?/>? ??
  73. ????????????<tx:method?name="remove*"??
  74. ????????????????rollback-for="Exception"/>? ??
  75. ????????????<tx:method?name="add*"??
  76. ????????????????no-rollback-for="Exception"?/>? ??
  77. ????????</tx:attributes>? ??
  78. ????</tx:advice>? ??
  79. ????<!--Transaction?-->? ??
  80. ???? ??
  81. ????<!--?DAO?--> ??
  82. ????<bean?id="genericDao"?lazy-init="true"?abstract="true"??
  83. ????????class="com.logcd.bo.dao.impl.GenericDaoImpl"> ??
  84. ????????<property?name="sessionFactory"> ??
  85. ????????????<ref?local="sessionFactory"?/> ??
  86. ????????</property> ??
  87. ????</bean> ??
  88. ??
  89. ????<bean?id="customersDao"?parent="genericDao"??
  90. ????????class="com.logcd.bo.dao.impl.CustomersDaoImpl"?/> ??
  91. ??
  92. ????<bean?id="customerDao"?parent="genericDao"??
  93. ????????class="com.logcd.bo.dao.impl.CustomerDaoImpl"?/> ??
  94. ??
  95. ????<bean?id="addressDao"?parent="genericDao"??
  96. ????????class="com.logcd.bo.dao.impl.AddressDaoImpl"?/> ??
  97. ??
  98. ????<bean?id="customerManageService"??
  99. ????????class="com.logcd.business.service.impl.CustomerManageServiceImpl"??
  100. ????????autowire="byName"/> ??
  101. ???????? ??
  102. </beans>??
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<description>springApp</description>	
	<!-- dataSource for MySQL -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/springapp" />
		<property name="username" value="root" />
		<property name="password" value="****" />
	</bean>	

	<!-- Hibernate SessionFactory for MySQL -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />

		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:/</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.jdbc.fetch_size">50</prop>
				<prop key="hibernate.jdbc.batch_size">100</prop>
			</props>
		</property>

	</bean>


	<!--Transaction --> 
	<bean id="txManager" 
		class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
			<property name="sessionFactory" ref="sessionFactory" /> 
	</bean> 

	<aop:config> <!--use CGLIB:proxy-target-class="true-->
		<aop:pointcut id="serviceOperator" expression="execution(* com.logcd.business.service.*.*(..))"/>
		<aop:advisor  advice-ref="txAdvice" pointcut-ref="serviceOperator"/> 
		<!--
		<aop:advisor pointcut="execution(* com.logcd.business.service..*Service.*(..))" advice-ref="txAdvice"/>       
		-->
	</aop:config> 
	
	<tx:advice id="txAdvice" transaction-manager="txManager"> 
		<tx:attributes> 
			<tx:method name="find*" read-only="true" /> 
			<tx:method name="load*" read-only="true" /> 
			<tx:method name="is*" read-only="true"/> 
			<tx:method name="save*" 
				rollback-for="Exception"/> 
			<tx:method name="insert*" 
				rollback-for="Exception" /> 
			<tx:method name="remove*"
				rollback-for="Exception"/> 
			<tx:method name="add*"
				no-rollback-for="Exception" /> 
		</tx:attributes> 
	</tx:advice> 
	<!--Transaction --> 
	
	<!-- DAO -->
	<bean id="genericDao" lazy-init="true" abstract="true"
		class="com.logcd.bo.dao.impl.GenericDaoImpl">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<bean id="customersDao" parent="genericDao"
		class="com.logcd.bo.dao.impl.CustomersDaoImpl" />

	<bean id="customerDao" parent="genericDao"
		class="com.logcd.bo.dao.impl.CustomerDaoImpl" />

	<bean id="addressDao" parent="genericDao"
		class="com.logcd.bo.dao.impl.AddressDaoImpl" />

	<bean id="customerManageService"
		class="com.logcd.business.service.impl.CustomerManageServiceImpl"
		autowire="byName"/>
		
</beans>


(2)测试
Java代码 复制代码
  1. package?com.logcd.test; ??
  2. ??
  3. import?org.springframework.context.ApplicationContext; ??
  4. import?org.springframework.context.support.ClassPathXmlApplicationContext; ??
  5. ??
  6. import?com.logcd.bo.Customers; ??
  7. import?com.logcd.business.service.CustomerManageService; ??
  8. ??
  9. import?junit.framework.TestCase; ??
  10. ??
  11. public?class?SpringServiceTest?extends?TestCase?{ ??
  12. ??
  13. ????private?CustomerManageService?customerManageService;? ??
  14. ???? ??
  15. ????protected?void?setUp()?throws?Exception?{ ??
  16. ????????super.setUp(); ??
  17. ????????ApplicationContext?app?=?new?ClassPathXmlApplicationContext("appContext.xml"); ??
  18. ????????customerManageService?=?(CustomerManageService)?app.getBean("customerManageService"); ??
  19. ????} ??
  20. ??
  21. ????protected?void?tearDown()?throws?Exception?{ ??
  22. ????????super.tearDown(); ??
  23. ????} ??
  24. ??
  25. ????public?void?testService()?throws?Exception{ ??
  26. ????????Customers?cus?=?new?Customers(); ??
  27. ????????cus.setName("testService"); ??
  28. ????????cus.setAge(29); ??
  29. ????????customerManageService.saveCustomers(cus); ??
  30. ????} ??
  31. }??
package com.logcd.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.logcd.bo.Customers;
import com.logcd.business.service.CustomerManageService;

import junit.framework.TestCase;

public class SpringServiceTest extends TestCase {

	private CustomerManageService customerManageService; 
	
	protected void setUp() throws Exception {
		super.setUp();
		ApplicationContext app = new ClassPathXmlApplicationContext("appContext.xml");
		customerManageService = (CustomerManageService) app.getBean("customerManageService");
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testService() throws Exception{
		Customers cus = new Customers();
		cus.setName("testService");
		cus.setAge(29);
		customerManageService.saveCustomers(cus);
	}
}


附:pointcut里的语法
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)其中带问号的modifiers-pattern?(public/protected) 和 declaring-type-pattern? throws-pattern? 可以不填

如execution(* *..BookManager.save(..))

第一颗* 代表ret-type-pattern 返回值可任意,
*..BookManager 代表任意Pacakge里的BookManager类。
如果写成com.xyz.service.* 则代表com.xyz.service下的任意类
com.xyz.service..* com.xyz.service则代表com.xyz.service及其子package下的任意类
save代表save方法,也可以写save* 代表saveBook()等方法
(..) 匹配0个参数或者多个参数的,任意类型
(x,..) 第一个参数的类型必须是X
(x,,,s,..) 匹配至少4个参数,第一个参数必须是x类型,第二个和第三个参数可以任意,第四个必须是s类型。