`

Spring Web 中模块化配置文件的加载

 
阅读更多

Spring中配置文件的读取:
在分层的软件体系结构中,有数据访问层,业务逻辑层,表现层,安全层。实现各层功能的一系列Bean均需要进行配置.
如果将整个应用程序的Bean都配置在applicationContext.xml一个文件中,这将使维护工作相当困难。
所以可以将各层的bean分别配置在各层的xml配置文件中:
比如一个onlineStore应用中,可以创建如下xml配置文件:
onlineStore-servlet.xml---------------配置Web层组件(Controller,ViewResolver,HandlerMapping等)
onlineStore-service.xml---------------服务层Bean
onlineStore-data.xml-----------------数据访问层
onlineStore-security.xml-------------安全控制层Bean

然后在web.xml中配置一个Context Loader
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

此外还要告诉这个ContextLoaderListener去哪里找Spring 的配置文件:
默认是找/WEB-INF/applicationContext.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/onlineStore-service.xml
/WEB-INF/onlineStore-security.xml
/WEB-INF/onlineStore-data.xml
</param-value>

onlineStore-servlet.xml未配置是因为DiapatcherServlet在初始化的时候会去
/WEB-INF/下加载该配置文件。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics