spring利用aop横切配置事务

xml中的配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         https://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop
 9         https://www.springframework.org/schema/aop/spring-aop.xsd
10         http://www.springframework.org/schema/tx
11         https://www.springframework.org/schema/tx/spring-tx.xsd">
12 
13     <!-- DataSource:使用Spring的数据源替换Mybatis的配置  c3p0 dbcp druid
14     这里使用Spring提供的jdbc-->
15     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
17         <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
18         <property name="username" value="root"/>
19         <property name="password" value="hahaha"/>
20     </bean>
21 
22     <!-- sqlSessionFactory   -->
23     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
24         <property name="dataSource" ref="dataSource"/>
25         <!--  绑定Mybatis配置文件      -->
26         <property name="configLocation" value="classpath:mybatis-config.xml"/>
27         <property name="mapperLocations" value="classpath:com/lieyan/mapper/*.xml"/>
28     </bean>
29 
30     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
31         <constructor-arg index="0" ref="sqlSessionFactory"/>
32     </bean>
33 
34     <!-- 配置声明式事务   -->
35     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
36         <property name="dataSource" ref="dataSource"/>
37     </bean>
38 
39     <!-- 结合aop实现事务的织入   -->
40     <!-- 配置事务通知   -->
41     <tx:advice id="txAdvice" transaction-manager="transactionManager">
42         <!-- 给哪些方法配置事务        -->
43         <!-- 配置事务的传播特性        -->
44         <tx:attributes>
45             <tx:method name="add" propagation="REQUIRED"/>
46             <tx:method name="delete" propagation="REQUIRED"/>
47             <tx:method name="update" propagation="REQUIRED"/>
48             <tx:method name="query" read-only="true"/>
49             <tx:method name="*" propagation="REQUIRED"/>
50         </tx:attributes>
51     </tx:advice>
52 
53     <aop:config>
54         <aop:pointcut id="txPointCut" expression="execution(* com.lieyan.mapper.*.*(..))"/>
55         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
56     </aop:config>
57 
58 </beans>

 

posted @ 2021-09-15 23:37  现在开始JAVA  阅读(80)  评论(0)    收藏  举报