原创文章,鼓励转载,请务必注明出处,作者:李佳
1.方法一, 使用TransactionProxyFactoryBean
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value/>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/UserInfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id="exampleManager">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="exampleManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="com.service.spring.impl.ExampleManagerImpl" id="exampleManagerTarget">
<property name="userinfo">
<ref local="userinfoDAO"/>
</property>
</bean>
<bean class="com.service.dao.impl.UserInfoDAOImpl" id="userinfoDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
2.方法二,使用ProxyFactoryBean
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value/>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/UserInfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean
class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean" id="baseUserService">
<property name="interceptorNames">
<list>
<value>txInterceptor</value>
</list>
</property>
</bean>
<bean id="userService" parent="baseUserService">
<property name="proxyInterfaces">
<value>com.bo.UserService</value>
</property>
<property name="target">
<bean class="com.bo.impl.UserServiceImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
</property>
</bean>
<bean class="com.dao.impl.UserDAOImpl" id="userDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
3.方法三,使用BeanNameAutoProxyCreator
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value/>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/UserInfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean
class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>txInterceptor</value>
<!-- add new Interceptor -->
</list>
</property>
<!--
<property name="beanNames">
<value>*Service,*Manager</value>
</property>
-->
<property name="beanNames">
<list>
<idref local="userService"/>
<!-- add new Service -->
</list>
</property>
</bean>
<!-- Start UserService -->
<bean class="com.bo.impl.UserServiceImpl" id="userService">
<property name="userDAO" ref="userDAO"/>
</bean>
<bean class="com.dao.impl.UserDAOImpl" id="userDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>