# Spring教程 - 12 注解方式实现声明式事务

下面接收使用 XML 的方式,了解一下就好了,使用上一章讲解的就好了。


其他的我就省略了,主要是 bean.xml 的配置:

<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           https://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           https://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1. 读取配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 2. 开启组件扫描,如果没有用到任何注解,可以不用配置 -->
    <context:component-scan base-package="com.foooor.hellospring"/>

    <!-- 3. 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 4. JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 5. 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 6. 事务通知(统一回滚策略) -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有方法出现 Exception 都回滚 -->
            <tx:method name="*" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

    <!-- 7. AOP 绑定事务 -->
    <aop:config>
        <!-- 对 com.foooor.hellospring.service 包下的所有方法进行事务管理 -->
        <aop:pointcut id="serviceMethods" expression="execution(* com.foooor.hellospring.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>

    <!-- 8. DAO Bean -->
    <bean id="userDao" class="com.foooor.hellospring.dao.impl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <!-- 9. Service Bean -->
    <bean id="userService" class="com.foooor.hellospring.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

     <!-- 10. Controller Bean -->
    <bean id="userController" class="com.foooor.hellospring.controller.UserController">
        <property name="userService" ref="userService"/>
    </bean>

</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

上面进行了各种配置,tx:advice 是用来做事务配置的,这里需要用到 AOP,所以项目中需要添加 AOP 的依赖。

<!-- 引入spring aop依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>6.2.9</version>
</dependency>

<!-- 引入spring aspects依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>6.2.9</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13

测试的时候,需要去掉 Controller、Service、Dao上的注解。


上面基本全是 XML 的配置。

还有一些人使用 XML 和注解的混合配置,那么可以使用如下配置:

<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           https://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           https://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1. 读取配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 2. 开启组件扫描,如果没有用到任何注解,可以不用配置 -->
    <context:component-scan base-package="com.foooor.hellospring"/>

    <!-- 3. 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 4. JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 5. 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 6. 开启基于注解的事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="userDao" class="com.foooor.hellospring.dao.impl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <bean id="userService" class="com.foooor.hellospring.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <bean id="userController" class="com.foooor.hellospring.controller.UserController">
        <property name="userService" ref="userService"/>
    </bean>

</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

在上面使用了 tx:annotation-driven 开启了 @Transactional 注解配置,所以是可以在类上添加 @Transactional 注解的,通过注解实现事务管理的,同时还使用了部分 XML 的相关配置。


根据自己喜欢的方式,选择注解或 XML 方式吧。