五、配置Spring
1. src下建立
       applicationContext.xml
       applicationContext_dao.xml
       applicationContext_action.xml
内容都为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

<beans>
</beans>
struts-config.xml中配置
<controller
    processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
  </controller>

  <message-resources
    parameter="com.test.struts.ApplicationResources">
  </message-resources>

  <plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
      value="/WEB-INF/applicationContext.xml,    
    /WEB-INF/applicationContext_dao.xml,    
    /WEB-INF/applicationContext_action.xml," />
  </plug-in>
3. applicationContext.xml 中配置spring管理hibernate
<?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.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
    <list>
      <value>userServiceDaoImp</value>
    </list>
  </property>
  <property name="interceptorNames">
    <list>
      <value>transactionInterceptor</value>
    </list>
  </property>
</bean>


<!-- 事务拦截器 -->
<bean id="transactionInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
    <ref bean="hibernatetransactionManager"/>
  </property>
  <property name="transactionAttributes">
    <props>
      <prop key="add*">PROPAGATION_REQUIRED,-MyException</prop>
      <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
  </property>
</bean>


<!-- hibernate事务管理器 -->

<bean id="hibernatetransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
    <ref bean="sessionFactory"/>
  </property>

</bean>

<!-- session工厂 -->
    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">

  <value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!--    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

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

  <property name="hibernateProperties">
    <props>
      <prop key="show_sql">true</prop>
    </props>
  </property>

  <property name="mappingResources">
    <list>
      <value>com/test/pojo/User.hbm.xml</value>
      <value>com/test/pojo/Role.hbm.xml</value>
      <value>com/test/pojo/Privilege.hbm.xml</value>
    </list>
  </property>

</bean>




<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">    
  <property name="driverClass">
    <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="jdbcUrl">
    <value>jdbc:mysql://localhost:3306/b2c2</value>
  </property>
  <property name="user">
    <value>root</value>
  </property>
  <property name="password">
    <value>root</value>
  </property>
</bean>

-->
</beans>
4.applicationContext_dao.xml 中配置对dao的注入
<?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="userServiceDaoImp" class="com.test.servicedaoimp.UserServiceImp">
  <property name="userdao">
    <ref bean="userDaoImp"/>
  </property>
  <property name="menudao">
    <ref bean="menuDaoImp"/>
  </property>
    
</bean>

<bean id="userDaoImp" class="com.test.imp.UserDaoImp">
  <property name="sessionFactory">
    <ref bean="sessionFactory"/>
  </property>
</bean>

<bean id="menuDaoImp" class="com.test.imp.MenuDaoImp">
  <property name="sessionFactory">
    <ref bean="sessionFactory"/>
  </property>
</bean>

</beans>
 
5. applicationContext_action.xml 中配置ActionServiceDao的注入
<beans>    
<bean name=\"/login\" class=\"com.test.struts.action.LoginAction\" singleton=\"false\">    
    <property name=\"userServiceDao\">    
        <ref bean=\"userServiceDaoImp\"/>    
    </property>    
</bean>    
</beans>