Loading

Spring xml配置文件结合AOP实现 声明式事务

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:contex="http://www.springframework.org/schema/tool"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:property-placeholder location="classpath*:**/*.properties" />
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataScource"/>
            <property name="mapperLocations" value="classpath:com/service/*.xml"/>
            <property name="typeAliasesPackage"  value="com.pojo">
            </property>
        </bean>

    <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSession"/>
    </bean>
    <bean id="dataScource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    <bean id="UserService" class="com.service.impl.UserServiceMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSession"></property>
    </bean>
<!--    开启 Spring 的事务处理功能   -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataScource"/>
    </bean>
<!--    将事务交给 spring 管理-->
<!--    <tx:jta-transaction-manager />-->
<!--    结合aop 实现事务的织入-->
    <tx:advice id="txadvice" transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pointcut01" expression="execution(* com.service.*.*(..))"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut01"/>
    </aop:config>
</beans>
posted @ 2022-09-16 17:41  冰莫莫  阅读(47)  评论(0编辑  收藏  举报