隨筆-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
          主站蜘蛛池模板: 淄博市| 无锡市| 扶余县| 清丰县| 灌南县| 那曲县| 沙河市| 定西市| 于都县| 光泽县| 淄博市| 新闻| 盐山县| 二手房| 南昌县| 龙游县| 潜江市| 渝北区| 全南县| 新巴尔虎左旗| 育儿| 扎囊县| 荔浦县| 凌海市| 辽阳县| 靖西县| 清徐县| 潜山县| 枣强县| 灌阳县| 香港 | 富蕴县| 射阳县| 浪卡子县| 隆安县| 江孜县| 白银市| 资溪县| 理塘县| 恩平市| 西安市|