spring来配置事务管理,记住三个参数就可以了
<aop:pointcut>切入点 <aop:advice>需要注入的方法 <aop:advisor> 适配器,把注入的方法注入切入点的位置,是连接pointcut跟advice的工具
下面上实例
<aop:config>
<aop:pointcut expression="execution(public * com.test.registration.service.*.*(..))"
id="logPointcut" />
<aop:advisor pointcut-ref="logPointcut"
advice-ref="txAdvice"
/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:pointcut expression="execution(public * com.test.registration.service.*.*(..))"
这句话对切入点执行方法的解析public * com.test.registration.service.*.*(..)
第一个*表示返回值为任意值
第二个*表示在 com.test.registration.service这个包中的任意类
第三个*表示当前类的任意方法(..)表示0个或多个参数
对于上边的实例tx:advice的配置解析事物管理来自txManager 而sessionfactory具体就不在详述了主要是配置数据源跟实体类之类的
tx:attributes里的方法,这里主要对增删改查进行了配置,
有的初学者会问,什么是事物呢,事物简单讲就是对多个方法之类的同时操作,遇到错误同时回滚,在这里是为了避免数据提交,
比如一个方法里写了好多个insert,有一个突然由于一些故障,insert不了了,那么直接插入的话,其结果肯定不是自己想要的了,
数据库里出现了一些脏数据。对于里边还有的参数解析,详见spring api.