不灭的火

革命尚未成功,同志仍须努力下载JDK17

作者:AlbertWen  添加时间:2021-03-28 21:17:15  修改时间:2025-03-10 21:26:51  分类:07.Java框架/系统  编辑

在web.xml中通过contextConfigLocation配置spring,contextConfigLocation参数定义了要装入的 Spring 配置文件。

如果想装入多个配置文件,可以在 <param-value> 标记中用逗号作分隔符。

在web.xml里配置Listener

<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

如果在web.xml里给该Listener指定要加载的xml,如:

<!-- spring config -->
<context-param>
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext.xml</param-value>
</context-param> 

则会去加载相应的xml,而不会加载 /WEB-INF 下的 applicationContext.xml。

但是,如果没有指定的话,默认会加载 /WEB-INF 下的 applicationContext.xml。

 

在一个团队使用Spring的实际项目中,应该需要多个Spring的配置文件,如何使用和交叉引用的问题:

多个配置文件可以在web.xml里用逗号分隔写入,如:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        applicationContext-database.xml, applicationContext.xml
    </param-value>  
</context-param>

多个配置文件里的交叉引用可以用ref的external或bean解决,例如:

applicationContext.xml

<bean id="userService" class="domain.user.service.impl.UserServiceImpl"> 
    <property name="dbbean">
        <ref bean="dbBean"/>
    </property> 
</bean>

dbBean在applicationContext-database.xml

 

 

摘自:https://blog.csdn.net/zhangliao613/article/details/6289114