AOP事务测试

AOP事务配置的3种方式

注解和xml结合方式

  • 导包
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.18.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.18.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.18</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <!--        AOP 框架-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.14</version>
    </dependency>

</dependencies>
  • spring配置文件
<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!--配置包扫描-->
    <context:component-scan base-package="com.hua"/>
    <!--数据库连接池配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"/>
        <property name="password" value=""/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?charsetEncoding=utf-8"/>
    </bean>
    <!--配置spring jdbctemplate 使用c3p0 数据源-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--    事物管理器配置-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="manager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启事物注解-->
    <tx:annotation-driven/>
</beans>
  • 然后就可以愉快的在需要事务的类和方法上使用@Transactional(value="manager")注解了 value的值是xml配置的事务管理器的id,如果没有指定value则默认value="transactionManager" 在xml中事务管理器的id需要使用默认的transactionManager

xml文件配置方式

  • 导包 参考注解导包
  • spring配置文件
<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!--配置包扫描-->
    <context:component-scan base-package="com.hua"/>
    <!--数据库连接池配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"/>
        <property name="password" value=""/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?charsetEncoding=utf-8"/>
    </bean>
    <!--配置spring jdbctemplate 使用c3p0 数据源-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--    事物管理器配置-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="manager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--事物通知方法-->
    <tx:advice id="txadvice" transaction-manager="manager">
        <tx:attributes>
            <tx:method name="user*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--    切面和切点配置 -->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.hua.service.UserService.*(..))"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
    </aop:config>
</beans>
  • 在xml中配置要使用事务的方法 和 事物属性 <tx:method name="user*" propagation="REQUIRED"/>

全注解方式

  • 创建一个配置类
package com.hua.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration // 配置类
@ComponentScan(basePackages="com.hua") // 配置包扫描
@EnableTransactionManagement  // 开启事务注解
public class TxConfig {


    // datasource config
    @Bean
    public ComboPooledDataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?charsetEncoding=utf-8");
        return dataSource;
    }
    // JdbcTemplate config
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        // dataSource 这个参数springioc会依据class来注入
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    // DataSourceTransactionManager config
    @Bean
    public DataSourceTransactionManager getTransactionManager(DataSource dataSource) {

        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;

    }
}
  • 使用注解测试
@Transactional()
public void userAccount() {
    userDao.addMoney();
    int a = 1 / 0;
    userDao.jianMoney();
}
===================================
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.userAccount();
posted @ 2021-07-09 11:49  huameixiao  阅读(60)  评论(0编辑  收藏  举报