spring-註解----transaction

      spring-註解----transactionjava

 

 package com.zwj.config;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator;
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.PlatformTransactionManager;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;
import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector;
import org.springframework.transaction.annotation.Transactional;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 聲明式事務:
 * 
 * 環境搭建:
 * 一、導入相關依賴
 *         數據源、數據庫驅動、Spring-jdbc模塊
 * 二、配置數據源、JdbcTemplate(Spring提供的簡化數據庫操做的工具)操做數據
 * 三、給方法上標註 @Transactional 表示當前方法是一個事務方法;
 * 四、 @EnableTransactionManagement 開啓基於註解的事務管理功能;
 *         @EnableXXX
 * 五、配置事務管理器來控制事務;
 *         @Bean
 *         public PlatformTransactionManager transactionManager()
 * 
 * 
 * 原理:
 * 1)、@EnableTransactionManagement
 *             利用TransactionManagementConfigurationSelector給容器中會導入組件
 *             導入兩個組件
 *             AutoProxyRegistrar
 *             ProxyTransactionManagementConfiguration
 * 2)、AutoProxyRegistrar:
 *             給容器中註冊一個 InfrastructureAdvisorAutoProxyCreator 組件;
 *             InfrastructureAdvisorAutoProxyCreator:?
 *             利用後置處理器機制在對象建立之後,包裝對象,返回一個代理對象(加強器),代理對象執行方法利用攔截器鏈進行調用;
 * 
 * 3)、ProxyTransactionManagementConfiguration 作了什麼?
 *             一、給容器中註冊事務加強器;
 *                 1)、事務加強器要用事務註解的信息,AnnotationTransactionAttributeSource解析事務註解
 *                 2)、事務攔截器:
 *                     TransactionInterceptor;保存了事務屬性信息,事務管理器;
 *                     他是一個 MethodInterceptor;
 *                     在目標方法執行的時候;
 *                         執行攔截器鏈;
 *                         事務攔截器:
 *                             1)、先獲取事務相關的屬性
 *                             2)、再獲取PlatformTransactionManager,若是事先沒有添加指定任何transactionmanger
 *                                 最終會從容器中按照類型獲取一個PlatformTransactionManager;
 *                             3)、執行目標方法
 *                                 若是異常,獲取到事務管理器,利用事務管理回滾操做;
 *                                 若是正常,利用事務管理器,提交事務
 *             
 */
@EnableTransactionManagement
@ComponentScan("com.zwj.tx")
@Configuration
public class TxConfig {
    
    //數據源
    @Bean
    public DataSource dataSource() throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        return dataSource;
    }
    
    //
    @Bean
    public JdbcTemplate jdbcTemplate() throws Exception{
        //Spring對@Configuration類會特殊處理;給容器中加組件的方法,屢次調用都只是從容器中找組件
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return jdbcTemplate;
    }
    
    //註冊事務管理器在容器中
    @Bean
    public PlatformTransactionManager transactionManager() throws Exception{
        return new DataSourceTransactionManager(dataSource());
    }
    

}
TxConfig
package com.zwj.tx;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void insert(){
        String sql = "INSERT INTO `tbl_user`(username,age) VALUES(?,?)";
        String username = UUID.randomUUID().toString().substring(0, 5);
        jdbcTemplate.update(sql, username,19);
        
    }

}
UserDao
package com.zwj.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserService {
    
    @Autowired
    private UserDao userDao;
    
    @Transactional
    public void insertUser(){
        userDao.insert();
        //otherDao.other();xxx
        System.out.println("插入完成...");
        int i = 10/0;
    }

}
UserService
package com.zwj.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.zwj.config.TxConfig;
import com.zwj.tx.UserService;



public class IOCTest_Tx {
    
    @Test
    public void test01(){
        AnnotationConfigApplicationContext applicationContext = 
                new AnnotationConfigApplicationContext(TxConfig.class);
    
        UserService userService = applicationContext.getBean(UserService.class);
        
        userService.insertUser();
        applicationContext.close();
    }

}
IOCTest_Tx
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zwj</groupId>
  <artifactId>spring-annotation---transactions</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>





    </dependencies>
</project>
pom.xml
相關文章
相關標籤/搜索