天天看點

SSH架構搭配(四)

五、配置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>

2  在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

<!-- 對需要進行事務的對象進行攔截 -->

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

  <property name="beanNames">

    <list>

      <value>userServiceDaoImp</value>

    </list>

  </property>

  <property name="interceptorNames">

      <value>transactionInterceptor</value>

</bean>

<!-- 事務攔截器 -->

<bean id="transactionInterceptor"

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

  <property name="transactionManager">

    <ref bean="hibernatetransactionManager"/>

  <property name="transactionAttributes">

    <props>

      <prop key="add*">PROPAGATION_REQUIRED,-MyException</prop>

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

    </props>

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

<bean id="hibernatetransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory">

    <ref bean="sessionFactory"/>

<!-- session工廠 -->

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

<property name="configLocation">

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

</property>

<!--    

  <property name="dataSource">

    <ref bean="dataSource"/>

  <property name="hibernateProperties">

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

  <property name="mappingResources">

      <value>com/test/pojo/User.hbm.xml</value>

      <value>com/test/pojo/Role.hbm.xml</value>

      <value>com/test/pojo/Privilege.hbm.xml</value>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">    

  <property name="driverClass">

    <value>com.mysql.jdbc.Driver</value>

  <property name="jdbcUrl">

    <value>jdbc:mysql://localhost:3306/b2c2</value>

  <property name="user">

    <value>root</value>

  <property name="password">

-->

4.applicationContext_dao.xml 中配置對dao的注入

<bean id="userServiceDaoImp" class="com.test.servicedaoimp.UserServiceImp">

  <property name="userdao">

    <ref bean="userDaoImp"/>

  <property name="menudao">

    <ref bean="menuDaoImp"/>

<bean id="userDaoImp" class="com.test.imp.UserDaoImp">

<bean id="menuDaoImp" class="com.test.imp.MenuDaoImp">

5. 在applicationContext_action.xml 中配置Action中ServiceDao的注入

<beans>    

<bean name=\"/login\" class=\"com.test.struts.action.LoginAction\" singleton=\"false\">    

    <property name=\"userServiceDao\">    

        <ref bean=\"userServiceDaoImp\"/>    

    </property>    

</bean>