您的位置:首页 > 编程语言 > Java开发

spring零总

2015-07-31 11:43 561 查看
1、事务管理

配置了事务管理的切面,执行过程中即使数据发生了更改,不影响数据库中的数据。只有在方法执行完成后才提交到数据库,中途发生异常,则回滚至原来状态。


 <!-- 配置事务管理器 -->
  <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

  <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="service"
expression=" execution(* com.*.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="service" advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" read-only="true" /> <!-- 以get开头的方法中的Connection是readonly的 -->
<tx:method name="update*" rollback-for="Throwable"/><!-- 在update开头的方法中遇到异常(Throwable)就回滚 -->
<tx:method name="delete*" rollback-for="Throwable"/><!-- 在delete开头的方法中遇到异常(Throwable)就回滚 -->
<tx:method name="save*" rollback-for="Throwable"/><!-- 在save开头的方法中遇到异常(Throwable)就回滚 -->
<tx:method name="add*" rollback-for="Throwable"/><!-- 在add开头的方法中遇到异常(Throwable)就回滚 -->
       <tx:method name="*"/>
</tx:attributes>
</tx:advice>


2、P标签

先引入spring p标签,再进行使用,用法相当于<property>。

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">


bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: