Spring2.X以AspectJ 式AOP 配置事务
- <?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"/><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"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" ref="sessionFactory" /> </bean> <aop:config> <!--use CGLIB:proxy-target-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"/></property></bean><bean id="customersDao" parent="genericDao"/><bean id="customerDao" parent="genericDao"/><bean id="addressDao" parent="genericDao"/><bean id="customerManageService"src="/img/2012/09/19/2357404463.gif">
- 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类型。