Spring2.X以AspectJ 式AOP 配置事务
来源:互联网 发布:淘宝客服怎么联系啊 编辑:程序博客网 时间:2024/06/07 03:59
(1)配置:
Spring的事务管理是通过AOP代理实现的,其中的事务通知由元数据驱动。代理对象与事务元数据结合产生一个AOP代理,它使用一个PlatformTransactionManager实现,配合TransactionInterceptor,在方法调用前后实施事务。
<?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)测试
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类型。
- Spring2.X以AspectJ 式AOP 配置事务
- 使用AspectJ的AOP配置管理事务配置
- spring1.x spring2的声明式事务配置
- spring2.x的超easy事务配置。。。
- Spring2.x的事务配置策略
- spring2集成hibernate3之事务配置aop配置
- spring2.0 aop配置
- 配置Spring2.0声明式事务
- spring2.0 事务配置
- spring2.5+ 配置事务
- spring2.x使用aop实现声明式日志记录
- 基于@AspectJ配置Spring AOP
- Spring Aspectj AOP配置学习
- Spring AOP Aspectj 注解配置
- Spring配置---<aop:aspectj-autoproxy />
- Spring2.0 AOP @AspectJ注解定义切面学习示例。
- Spring2.5 基于AspectJ风格的AOP问题
- Spring4.X系列之AOP-@AspectJ
- js 鼠标左右点击事件
- JAVA 正则表达式 (超详细)
- Word2007 无法复制粘贴的原因以及正确加载mathtype
- STL标准库中的算法函数
- 我也写个学习日志吧
- Spring2.X以AspectJ 式AOP 配置事务
- UvaOJ 127 - "Accordian" Patience
- C++关键字
- [objective-C]setDateFormat
- 第八周任务二
- 如何反编绎apk文件
- android应用程序的混淆打包
- 如何防止代码被反编译
- loadrunner 迭代参数1 file类型