eclipse下maven管理Spring项目构:SpringAOP,基于XMl,基于注解声明事务,及事务的传播行为

一:在applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

 //包扫描
        <context:component-scan base-package="com.qst"/>

//开启注解
        <aop:aspectj-autoproxy proxy-target-class="true"/>
 //数据源。有三种数据库连接池的配置。 这个是用JDBC连接的Mysql
        <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/spring?characterEncoding=utf8"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

//以上不管是基于XML还是基于注解都需要

//基于注解需要加入此句
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <tx:annotation-driven transaction-manager="txManager"/>
 //基于XML
        <!-- 事务通知 -->
        <!-- <tx:advice id="myAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="add" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="RuntimeException"/>
                <tx:method name="addlog" propagation="REQUIRES_NEW"/>//propagation事物的传播行为
                <tx:method name="delete*"/>
                <tx:method name="update*"/>
                <tx:method name="select*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        <aop:config>
            <aop:pointcut expression="execution(* com.qst.service.*.*(..))" id="mytxpointcut"/>
            <aop:advisor advice-ref="myAdvice" pointcut-ref="mytxpointcut"/>
        </aop:config> -->
        
        <!-- <bean id="business" class="com.qst.BusinessTest"></bean>
        
        <bean id="aspect" class="com.qst.AspectAop"></bean>
        
//SpringAOP        

<aop:config>
            <aop:aspect ref="aspect">
                <aop:pointcut expression="execution(* com.qst.*.*(..))" id="mypointcut"/>
                <aop:before method="before" pointcut-ref="mypointcut"/>
                <aop:after method="after" pointcut-ref="mypointcut"/>
                <aop:after-returning method="afterReturning" pointcut-ref="mypointcut"/>
                <aop:after-throwing method="except" pointcut-ref="mypointcut" throwing="e"/>
                <aop:around method="around" pointcut-ref="mypointcut"/>
            </aop:aspect>
        </aop:config> -->
        
    <!-- <bean id="helloTest" class="com.qst.HelloTest" scope="prototype" init-method="init" destroy-method="destroy"/>
    
    <bean id="staticFactory" class="com.qst.SingleStaticFactoryBean" factory-method="getSingleTest"/>
    
    <bean id="factory" class="com.qst.FactoryBean"></bean>
    <bean id="test" factory-bean="factory" factory-method="getSingTest"></bean>
    
    <bean id="userDao" class="com.qst.dao.UserDaoImpl"></bean>
    
    <bean id="userService" class="com.qst.service.UserServiceImpl">
        <constructor-arg index="0" ref="userDao"></constructor-arg>
    </bean>
    
    <bean id="userService" class="com.qst.service.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean> -->
    <!-- <bean id="userController" class="com.qst.controller.UserController">
        <property name="list">
            <list>
                <value></value>
                <bean></bean>
            </list>
        </property>
        <map></map>
        <set></set>
    </bean> -->
</beans>

事务传播行为类型

说明

PROPAGATION_REQUIRED

如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

PROPAGATION_SUPPORTS

支持当前事务,如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY

使用当前的事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW

新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED

以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER

以非事务方式执行,如果当前存在事务,则抛出异常。

PROPAGATION_NESTED

如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值