<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="employeeDAO" class="com.sodi.dao.EmployeeDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="departmentDAO" class="com.sodi.dao.DepartmentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 1、增加声明式事务处理 (注意:这里AOP的jar包必须导入,否则配置文件出错)
1.1 先配置事务管理器: -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 1.2 再配置事务拦截器: -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<!-- 下面定义事务属性增删改需要事务,而选择不需要事务
表示以下面开头的方法才可以通过事务,故取名时注意
-->
<props>
<prop key="attachDirty">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly
</prop>
</props>
</property>
</bean>
<!--
1.2
应用事务拦截器,去拦截service或dao:一般情况下是拦截service类,只有项目中没有service层的时候,才去拦截dao层的类
-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>employeeDAO</value>
<value>departmentDAO</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 这里是事务拦截器 -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean name="/employeeMain" class="com.sodi.action.EmployeeMainAction">
<property name="empDao">
<ref bean="employeeDAO" />
</property>
<property name="depDao">
<ref bean="departmentDAO" />
</property>
</bean>
</beans>