Spring声明式事务管理(基于TransactionProxyFactoryBean)

来源:互联网 发布:手机搬家软件 编辑:程序博客网 时间:2024/05/22 01:51

加载数据库连接需要的properties文件:

<context:property-placeholder location="classpath:jdbc.properties"/>  


第一步:通过c3p0连接池配置dataSource

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">      <property name="driverClass" value="${jdbc.driver}"/>      <property name="jdbcUrl" value="${jdbc.url}"/>      <property name="user" value="${jdbc.user}"/>      <property name="password" value="${jdbc.password}"/>  </bean>  
第二步:配置Dao数据库依赖

<bean id="accountDao" class="com.sunjian.dao.impl.AccountDaoImpl">      <property name="dataSource" ref="dataSource"/>  </bean>  
第三步:根据数据库连接池dataSource配置事务管理器

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean>
第四步:配置TransactionProxyFactoryBean
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager" ref="transactionManager"/><property name="target" ref="accountService"/><property name="transactionAttributes"><props><!-- prop格式             PROPAGATION:     事务传播行为             ISOLATION:       事务隔离级别             readOnly:        只读             -Exception:      发生哪些异常进行回滚             +Exception:      发生哪些异常不进行回滚--><prop key="transfor">PROPAGATION_REQUIRED</prop></props></property></bean>

第五步:测试中将原本注入service的地方改为注入代理accountServiceProxy

@Resource(name="accountServiceProxy")private AccountService accountService;

阅读全文
0 0
原创粉丝点击