Spring 声明式事务的配置方式(一)

1.首先定义DataSource

        <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

        <property name="url" value="jdbc:mysql://hostname:post/dbname"></property>

        <property name="username" value="username"></property>

        <property name="password" value="pwd"></property>

   </bean>

2.定义SessionFactory       

       <bean id="sessionFactory"

                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <!-- 注入DataSource -->

        <property name="dataSource" ref="dataSource"></property>

        <property name="hibernateProperties">

           <props>

               <prop key="hibernate.show_sql">true</prop>

               <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

           </props>

        </property>

        <property name="mappingDirectoryLocations"

                    value="classpath:/org/companyname/myprj/entities"></property>

   </bean>

3.定义事务管理器

<bean id="transactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"></property>

   </bean>

4.持久层

<bean id="daoTemplate" class="org.companyname.myprj.dal.DaoTemplate"

         autowire="byName"></bean>

5.业务模块

<bean id="usersService" class="org.companyname.myprj.services.impl.UserService">

        <property name="daoTemplate" ref="daoTemplate"></property>

</bean>

 

<bean id="itemsService" class="org.companyname.myprj.services.impl.ItemService">

        <property name="daoTemplate" ref="daoTemplate"></property>

</bean>

6.开始配置声明式事务

<bean id="usersBiz"

        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

        <property name="proxyInterfaces">

           <list>

               <value>org.companyname.myprj.services.IUsersService</value>

           </list>

        </property>

                  <property  name="transactionAttributeSource" 

                                ref="transactionAttributeSource"></property>

        <property name="target" ref="usersService"></property>

        <property name="transactionManager" ref="transactionManager"></property>

   </bean>

 

<bean id="itemsBiz" 

     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

        < property name="proxyInterfaces">

           <list>

               <value>org.companyname.myprj.services.IItemsService</value>

           </list>

        </property>

                   <property name="transactionAttributeSource"

                                ref="transactionAttributeSource"></property>

        <property name="target" ref="userService"></property>

        <property name="transactionManager" ref="transactionManager"></property>

   </bean>

   <!-- 配置事务传递属性 -->

<bean id="transactionAttributeSource"

       class="org.springframework.transaction.

                interceptor.NameMatchTransactionAttributeSource">

        <property name="properties">

           <props>

               <prop key="find*">PROPAGATION_SUPPORTS</prop>

               <prop key="get*">PROPAGATION_SUPPORTS</prop>

               <prop key="valid*">PROPAGATION_SUPPORTS</prop>

               <prop key="reg*">PROPAGATION_REQUIRED</prop>

               <prop key="modify*">PROPAGATION_REQUIRED</prop>

               <prop key="process*">PROPAGATION_REQUIRED</prop>

               <prop key="do*">PROPAGATION_SUPPORTS</prop>

           </props>

        </property>

   </bean>

这种方法针对每一个业务模块会配置一个业务代理服务,如果业务模块太多的话,代码量会显得很大,因此要开始考虑继承的思想。即采用第二种配置方法

你可能感兴趣的:(Spring 声明式事务的配置方式(一))