idea创建基于maven的ssm项目【5】配置applicationContext-trans.xml

本节操作完成了对spring父容器中事务层扫描加载的配置。

1、在resources中创建applicationContext-trans.xml

具体操作如下图所示:

idea创建基于maven的ssm项目【5】配置applicationContext-trans.xml

 

2、修改applicationContext-trans.xml

<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--设置事务管理器,引用的是applicationContext-dao.xml中配置的datasource-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务通知,引用上面设置的事务管理器
        add、delete、update开头的方法需要事务
        其他方法都是只读的
    -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--定义连接点,advisor定义了通知在连接点上执行即织入-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* net.wanho.service.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

至此,我们完成了对spring父容器中事务层扫描加载的配置。