详细介绍Spring使用多个xml配置文件的代码实例

互联网 17-3-8
Spring使用多个xml配置文件,有需要的朋友可以参考下。
<context-param><param-name>contextConfigLocation</param-name>  <param-value>classpath*:conf/spring/applicationContext_core*.xml,  classpath*:conf/spring/applicationContext_dict*.xml,classpath*:conf/spring/applicationContext_hibernate.xml,  classpath*:conf/spring/applicationContext_staff*.xml,classpath*:conf/spring/applicationContext_security.xml  classpath*:conf/spring/applicationContext_modules*.xmlclasspath*:conf/spring/applicationContext_cti*.xml  classpath*:conf/spring/applicationContext_apm*.xml</param-value>  </context-param>
<listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
<1-- XML 文件的文件头二〉<?xml version="l.O" encoding="工80-8859-1"?>  < 1-- web.xm1 文件的DTD 等信息一〉<!DOCTYPE web-app  PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN"''http://java.sun.com/dtd/web-app_2_3.dtd''>  <web-app><!一确定多个配置文件>  <context-param><1-- 参数名为contextConfigLocation…〉  <param-name>contextConfigLocation</param-name><!一多个配置文件之间以,隔开二〉  <param-value>/WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xml</param-value>  </context-param><!-- 采用listener创建Applicat工onContext 实例-->  <listener><listener-class>org.spr工ngframework.web.context.ContextLoader  Listener</listener-class></listener></web-app>
<servlet>  <servlet-name>context</servlet-name><servlet-class>org.springframework.web.context.ContextLoaderServlet</  servlet-class><load-on-startup>l</load-on-startup>  </servlet>
<!-- XML 文件的文件头--><?xml version="1.0" encoding="工SO-8859-1"?>  <! -- web.xml 文件的DTD 等信息→<!DOCTYPE web-appPUBLIC "-//Sun Microsystems , 工口c.//DTD Web Application 2.3//EN"  ''http://java.sun.com/dtd/web-app_2_3.dtd''><web-app>  <'一确定多个配置文件一><context-param>  <!-- 参数名为contextConfigLocation--><param-name>contextConfigLocation</param-name><!-- 多个配置文件之间以,隔开一〉  <param-value>/WEB-工NF/daoContext.xml, !WEB-工NF/applicationContext.xml</param-value>  </context-param><!一采用load-on-startup Servlet 创建Applicat工onContext 实例一〉  <servlet><servlet-narne>context</servlet-narne>  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  <!一下面值小一点比较合适,会优先加载一〉<load-on-startup>l</load-on-startup></servlet>  </web-app>

2, 使用匹配符

<context-param><param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext*.xml</param-value></context-param>

比 如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"><set-property property="contextConfigLocation"  value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,,,,,,,"/>

4,如果是非j2ee应用直接程序加载.

ApplicationContext act = new ClassPathXmlApplicationContext(new  String[]{"bean1.xml","bean2.xml"});BeanDefinitionRegistry reg = new DefaultListableBeanFactory();  XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);  reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));  reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));  BeanFactory bf = (BeanFactory)reg;

最后,解释以下classpath*:与classpath的区别:

classpath*:的出现是为了从多个jar文件中加载相同的文件.classpath:只能加载找到的第一个文件.

以上就是详细介绍Spring使用多个xml配置文件的代码实例的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:详细介绍使用XML资源文件来定义颜色,样式等

相关资讯