AOP realiza el control de transacciones

(1) Control declarativo de transacciones, implementación del modo de configuración

<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) Control declarativo de transacciones basado en anotaciones

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

<! - 配置 jdbcTemplate -> 
<bean id = " jdbcTemplate "class =" org.springframework.jdbc.core.JdbcTemplate "> 
    <propiedad nombre =" fuente de datos "ref =" fuente de datos "> </property> 
</bean>
<! - Fuente de datos de configuración -> 
<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <nombre de propiedad = "driverClassName" value = "com.mysql.jdbc.Driver"> </property> 
    <property name = "url" value = "jdbc: mysql: // localhost: 3306 / eesy"> </property> 
    <property name = "username" value = "root"> </property> 
    <propiedad name = "password" value = "1234"> </property> 
</bean> 
<! - Pasos de configuración del control de transacciones declarativas basado en anotaciones en la primavera 
    1. Configurar el administrador de transacciones, inyectar la fuente de datos 
    2. Activar la anotación de primavera Soporte de transacciones 
       tx: basado en anotaciones 
    3. Utilice anotaciones @Transactional donde se necesite soporte para transacciones 
    ->property name = "dataSource" ref = "dataSource"> </property> 
</bean> 
<! - Habilita el soporte de Spring para transacciones de anotación ->
 
<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <propiedad name = "dataSource" ref = "dataSource"> </property> 

<tx: administrador de transacciones basado en anotaciones = "transactionManager"> </ tx: basado en anotaciones>
@Repository ("accountDao") 
public class AccountDaoImpl implementa IAccountDao {// Quita extiende JdbcDaoSupport, escribe jdbcTemplate tú mismo 

    // Necesitas configurar jdbcTemplate en bean.xml 
    @Autowired 
    private JdbcTemplate jdbcTemplate;

 

@Service ("accountService") 
// 只读 型 事务 配置
@Transactional (propagation = Propagation.SUPPORTS, readOnly = true) 
clase pública AccountServiceImpl implementa IAccountService { 

    @Autowired 
    privado IAccountDao accountDao; 

// setAccountDao público vacío (IAccountDao accountDao) { 
// this.accountDao = accountDao; 
//}

 

// Requiere configuración de transacción de lectura-escritura 
@Transactional (propagation = Propagation.REQUIRED, readOnly = false) 
@Override 
public Account findAccountById (Integer accountId) { 
    return accountDao.findAccountById (accountId); 

}

 

Clase de prueba

@RunWith (SpringJUnit4ClassRunner.class) 
@ContextConfiguration (ubicaciones = "classpath: bean.xml") 
clase pública AccountServiceTest {
  
  
@Autowired 
private IAccountService como;

 

Supongo que te gusta

Origin blog.csdn.net/sky2line/article/details/109633085
Recomendado
Clasificación