spring幾種事務配置@以及spring所需jar包

y_keven發表於2013-04-24

根據代理機制不同,有以下幾種配置方式:

先定義一個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>


 

相關文章