Spring多配置文件

转自:http://www.blogjava.net/liuqiang5151/articles/143499.html

众所周知,Spring框架以对象的依赖注入而著名,通过applicationContext.xml配置,可以实现解耦合。如果我们把所有的对象都通过applicationContext.xml注入,那就会显得很臃肿,难于维护。对此,我们可以采用多配置文件相关联 。比如:在struts-config.xml中如下定义

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 

 <set-property property="contextConfigLocation" value="/WEB-INF/action-service.xml"/> 

</plug-in> 
在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> 

这样也实现了一种分层的思想,易于维护,看起来多爽。最后在web.xml中将其关联

<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" />要不然只能在本文件中找还有每个配置文件的名称空间一定要写。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值