隨筆-9  評論-168  文章-266  trackbacks-0

           眾所周知,Spring框架以對象的依賴注入而著名,通過applicationContext.xml配置,可以實現(xiàn)解耦合。如果我們把所有的對象都通過applicationContext.xml注入,那就會顯得很臃腫,難于維護。對此,我們可以采用多配置文件相關(guān)聯(lián) 。比如:在struts-config.xml中如下定義

          1<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
          2    <set-property property="contextConfigLocation" value="/WEB-INF/action-service.xml"/>
          3  </plug-in>
          4

          在action-service.xml中

          <bean name="/login" class="com.lq.struts.action.LoginAction"
            
          abstract="false" lazy-init="default"
            autowire
          ="default" dependency-check="default">
            
          <property name="userService">
             
          <ref bean="userService" />
            
          </property>
           
          </bean>

          在sp-service.xml中

          <bean id="userService" class="com.lq.service.impl.UserServiceImpl"
            lazy
          -init="default"
            autowire
          ="default" dependency-check="default">
            
          <property name="userinfoDAO">
             
          <ref bean="userinfoDAO" />
            
          </property>
           
          </bean>


          在applicationContext中

          <bean id="userinfoDAO" class="com.lq.dao.UserinfoDAO">
            
          <property name="sessionFactory">
             
          <ref bean="sessionFactory" />
            
          </property>
           
          </bean>

           

          這樣也實現(xiàn)了一種分層的思想,易于維護,看起來多爽。最后在web.xml中將其關(guān)聯(lián)

           

          <context-param>
            
          <param-name>contextConfigLocation</param-name>
            
          <param-value>
             
          /WEB-INF/sp-service.xml
             
          /WEB-INF/applicationContext.xml
            
          </param-value>
           
          </context-param>
           
          <listener>   
             
          <listener-class>   
              org.springframework.web.context.ContextLoaderListener   
             
          </listener-class>   
             
          </listener>

          注意兩點<ref bean="sessionFactory" />不能寫成<ref local="sessionFactory" />要不然只能在本文件中找
          還有每個配置文件的名稱空間一定要寫

          posted on 2010-04-01 23:05 紫蝶∏飛揚↗ 閱讀(807) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 温泉县| 平阳县| 靖安县| 大邑县| 新竹县| 常宁市| 孟村| 茌平县| 芦山县| 潼关县| 华阴市| 长春市| 通城县| 南丹县| 建始县| 东安县| 抚远县| 常熟市| 苏尼特右旗| 海门市| 台南县| 云阳县| 太湖县| 府谷县| 宁阳县| 体育| 吴川市| 怀仁县| 神农架林区| 安达市| 成武县| 姚安县| 二连浩特市| 特克斯县| 罗甸县| 镇巴县| 乌恰县| 西藏| 双江| 江达县| 榕江县|