spring幾種事務配置@以及spring所需jar包
根據代理機制不同,有以下幾種配置方式:
先定義一個DAO介面及實現類:
Java程式碼
public interface IUserDao {
public void insertUser(UserTable user);
}
public interface IUserDao {
public void insertUser(UserTable user);
}
實現類:
Java程式碼
public class UserDaoImpl extendsHibernateDaoSupport implements IUserDao{
public void insertUser(UserTable user) {
getHibernateTemplate().saveOrUpdate(user);
}
}
public class UserDaoImpl extendsHibernateDaoSupport implements IUserDao{
publicvoid insertUser(UserTable user) {
getHibernateTemplate().saveOrUpdate(user);
}
}
第一種
為每個bean設定一個代理,這種是根據具體需求來定,如要對具體到每個交易進行事務操作的話,這個方式是最合適的;
Java程式碼
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 每個bean都有一個代理 -->
<beans>
<!-- sessionFactory相當於spring datasource -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction2.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 每個bean都有一個代理<property name="target" ref="userDaoTarget" /> -->
<bean id="userDaoProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事務管理器 -->
<propertyname="transactionManager" ref="transactionManager" />
<property name="target"ref="userDao" />
<propertyname="proxyInterfaces" value="org.lgh.spring.transaction2.IUserDao"/>
<!-- 配置事務屬性 -->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 每個bean都有一個代理 -->
<beans>
<!--sessionFactory相當於springdatasource -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"value="classpath:hibernate.cfg.xml" />
<property name="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!--定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction2.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 每個bean都有一個代理 <propertyname="target" ref="userDaoTarget" /> -->
<bean id="userDaoProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事務管理器-->
<property name="transactionManager"ref="transactionManager" />
<property name="target" ref="userDao" />
<property name="proxyInterfaces"value="org.lgh.spring.transaction2.IUserDao" />
<!-- 配置事務屬性 -->
<property name="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
第二種:
所有的bean共享一個代理
Java程式碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 所有的bean共享一個代理-->
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一個代理/>-->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事務管理器 -->
<propertyname="transactionManager" ref="transactionManager" />
<!-- 配置事務屬性 -->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction3.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一個代理/>-->
<bean id="userDaoProxy"parent="transactionBase" >
<property name="target"ref="userDao" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 所有的bean共享一個代理-->
<beans>
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"value="classpath:hibernate.cfg.xml" />
<property name="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一個代理/> -->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事務管理器-->
<property name="transactionManager"ref="transactionManager" />
<!-- 配置事務屬性-->
<property name="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction3.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一個代理/> -->
<bean id="userDaoProxy" parent="transactionBase">
<property name="target" ref="userDao" />
</bean>
</beans>
第三種:
使用攔截器 來配置你的事務,這個主要是進行一些方法呼叫前後進行一些其他事件的處理,如進行許可權檢查等...
程式碼如下:
Java程式碼
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 使用攔截器 -->
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<propertyname="transactionManager" ref="transactionManager" />
<!-- 配置事務屬性 -->
<propertyname="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="logger"class="org.lgh.spring.transaction4.SpringAOPInterceptor">
</bean>
<bean id="logBeforeAdvice"class="com.spring.advices.LogBeforeAdvice">
</bean>
<!-- 定義BeanNameAutoProxyCreator,該bean是個bean後處理器,無需被引用,因此沒有id屬性
這個bean後處理器,根據事務攔截器為目標bean自動建立事務代理 -->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 加上此句就不會丟擲 $Proxy cannot be cast的異常啦-->
<propertyname="proxyTargetClass">
<value>true</value>
</property>
<property name="beanNames">
<list>
<!-- *Dao對應下面的userDao要對它進行攔截-->
<value>userDao</value>
</list>
</property>
<propertyname="interceptorNames">
<list>
<value>transactionInterceptor</value>
<value>logBeforeAdvice</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction4.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 服務層 service-->
<bean id="userService"class="org.lgh.spring.transaction4.UserServiceImpl">
<property name="userDao"ref="userDao" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 使用攔截器 -->
<beans>
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"value="classpath:hibernate.cfg.xml" />
<property name="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"ref="transactionManager" />
<!-- 配置事務屬性-->
<property name="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="logger"class="org.lgh.spring.transaction4.SpringAOPInterceptor">
</bean>
<bean id="logBeforeAdvice"class="com.spring.advices.LogBeforeAdvice">
</bean>
<!-- 定義BeanNameAutoProxyCreator,該bean是個bean後處理器,無需被引用,因此沒有id屬性
這個bean後處理器,根據事務攔截器為目標bean自動建立事務代理 -->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 加上此句就不會丟擲$Proxy cannot be cast的異常啦-->
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="beanNames">
<list>
<!-- *Dao對應下面的userDao要對它進行攔截-->
<value>userDao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
<value>logBeforeAdvice</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction4.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 服務層service-->
<bean id="userService"class="org.lgh.spring.transaction4.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
</beans>
第四種:
使用aop:config配置方式
Java程式碼
<?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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="org.lgh.spring.transaction5"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:configproxy-target-class="true" >
<aop:pointcutid="interceptorPointCuts"
expression="execution(*org.lgh.spring.transaction5.*.*(..))" />
<aop:advisoradvice-ref="txAdvice"
pointcut-ref="interceptorPointCuts"/>
</aop:config>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction5.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scanbase-package="org.lgh.spring.transaction5" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"value="classpath:hibernate.cfg.xml" />
<property name="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true" >
<aop:pointcutid="interceptorPointCuts"
expression="execution(* org.lgh.spring.transaction5.*.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction5.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
</beans>
還有一種就是採用全註解方式:
程式碼如下:
Java程式碼
<?xml version="1.0"encoding="UTF-8"?>
<!--全註解 -->
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scanbase-package="org.lgh.spring.transaction6" />
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction6.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!--全註解 -->
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scanbase-package="org.lgh.spring.transaction6" />
<tx:annotation-driven proxy-target-class ="true"transaction-manager="transactionManager"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(宣告式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction6.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
相關文章
- spring事務失效的幾種場景以及原因Spring
- Spring事務配置的五種方式Spring
- Spring配置事務Spring
- Spring的四種宣告式事務的配置-Hibernate事務Spring
- ssh所需jar包JAR
- SSH框架整合配置所需JAR包(SSH整合)框架JAR
- Spring AOP及事務配置三種模式詳解Spring模式
- @Transactional spring 配置事務 注意事項Spring
- Spring事務配置的五種方式和spring裡面事務的傳播屬性和事務隔離級別Spring
- spring宣告式事務管理配置Spring
- spring datasource 配置及事務管理Spring
- [摘]Spring的jar包解析SpringJAR
- 8個Spring事務失效的場景,你碰到過幾種?Spring
- Spring中如何配置Hibernate事務Spring
- spring 事務Spring
- spring事務Spring
- Spring事務失效的一種原因(this呼叫)Spring
- Spring事務需要注意的幾個點Spring
- 五(二)、spring 宣告式事務xml配置SpringXML
- Spring框架中配置事務管理器Spring框架
- Spring配置事務的五個常用方法Spring
- Spring 事務管理Spring
- Spring事務管理Spring
- Spring中的AOP,以及宣告式事務 @Transactional無法攔截事務Spring
- Spring 七種事務傳播性介紹Spring
- Day68 Spring TX (事務)和配置細節以及單例設計模式Spring單例設計模式
- Spring_11 Transaction(事務) 使用(XML 配置方式)SpringXML
- Spring Jar包因為JDK版本報錯SpringJARJDK
- Spring事務的介紹,以及基於註解@Transactional的宣告式事務Spring
- Spring Boot讀取配置檔案的幾種方式Spring Boot
- Spring事務專題(四)Spring中事務的使用、抽象機制及模擬Spring事務實現Spring抽象
- Spring宣告式事務注意點,以及不生效情況Spring
- Spring Boot 將jar包釋出方式改為War包Spring BootJAR
- Spring宣告式事務的兩種實現方式Spring
- Spring系列.事務管理Spring
- Spring事務筆記Spring筆記
- Spring系列-事務管理Spring
- Spring-AOP事務Spring