AOP realizes transaction control

(1) Declarative transaction control, configuration mode implementation

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置业务层-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置账户的持久层-->
    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>

    <!--spring中基于xml的声明式事务控制配置步骤
    1、配置事务管理器,注入数据源
    2、配置事务的通知,需要导入事务的约束
          配置事务的通知标签:tx:advice,tx名称空间和约束,同时也需要aop的
          使用tx:advice标签配置事务通知,属性
          id:给事务通知起一个唯一标识
          transaction-Manager:给事务通知提供一个事务管理器引用
    3、配置aop中的通用切入点表达式
    4、建立切入点表达式和事务通知的对应关系
    5、配置事务的属性
          在事务的通知tx:advice标签内部
          isolation="" :事务的隔离级别,默认值是DEFAULT,表示使用数据库的默认隔离级别
          propagation="" :事务的传播行为,默认值是REQUIRED,表示一定会有事务,增删改的选择,查询方法可以选择SUPPORTS
          read-only="" :事务是否只读,只有查询方法才能设置为true,默认值是false,表示读写
          timeout="":事务的超时时间,默认值是-1,表示永不超时,如果指定了数值,以秒为单位
          rollback-for="" :指定一个异常,当该异常发生时,事务回滚,产生其他异常时,事务不回滚。没有默认值,表示任何异常都回滚
          no-rollback-for="" :与rollback-for=""相反,指定一个异常,当该异常发生时,事务不回滚,产生其他异常时,事务回滚,没有默认值,表示任何异常都回滚
    -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--事务的属性-->
        <tx:attributes>
            <tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
            <tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
            <!--name="*":表示所有的方法都使用一样的事务,对于查询不适用,可以使用以下方法-->
            <tx:method name="find*" propagation="REQUIRED" read-only="true"></tx:method>
            <!--name="find*"优先级高于name="*",以后查询方法可以考虑使用find开头-->
        </tx:attributes>
    </tx:advice>

    <!--aop-->
    <aop:config>
        <!--切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"/>
        <!--建立切入点表达式和事务通知的对应关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>

(2) Declarative transaction control based on annotations

<!--配置自动扫描的包-->
<context:component-scan base-package="com.itheima"></context:component-scan>

<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Configuration data source--> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> 
    <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property> 
    <property name="username" value="root"></property> 
    <property name="password" value="1234"></property> 
</bean> 
<!---Annotation-based declarative transaction control configuration steps in spring 
    1. Configure transaction manager, inject data source 
    2. Turn on spring annotation Transaction support 
       tx:annotation-driven 
    3. Use @Transactional annotations where transaction support is needed 
    --> 

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"></property> 
</bean> 

<!--Enable spring support for annotation transactions-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
@Repository("accountDao") 
public class AccountDaoImpl implements IAccountDao {//Remove extends JdbcDaoSupport, write jdbcTemplate yourself 

    //You need to configure jdbcTemplate in bean.xml 
    @Autowired 
    private JdbcTemplate jdbcTemplate;

 

@Service("accountService")
//只读型事务配置
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AccountServiceImpl implements IAccountService{

    @Autowired
    private IAccountDao accountDao;

//    public void setAccountDao(IAccountDao accountDao) {
//        this.accountDao = accountDao;
//    }

 

//Requires read-write transaction configuration 
@Transactional(propagation = Propagation.REQUIRED,readOnly = false) 
@Override 
public Account findAccountById(Integer accountId) { 
    return accountDao.findAccountById(accountId); 

}

 

Test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
  
  
@Autowired
private  IAccountService as;

 

Guess you like

Origin blog.csdn.net/sky2line/article/details/109633085