`
Sev7en_jun
  • 浏览: 1212980 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
84184fc0-d0b6-3f7f-a3f0-4202acb3caf5
Apache CXF使用s...
浏览量:109897
社区版块
存档分类
最新评论

spring 加载多个配置文件

阅读更多

FROM: http://bluexp29.blog.163.com/blog/static/33858148200991622842425

 

加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet。推荐使用ContextLoaderListener
         这两者在功能上完全等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。开发中可根据目标Web容器的实际情况进行选择。

配置非常简单,在web.xml中增加:
<listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
</listener>
或:
<servlet>
         <servlet-name>context</servlet-name>
         <servlet-class>
           org.springframework.web.context.ContextLoaderServlet
         </servlet-class>
         <load-on-startup>1</load-on-startup>
</servlet>
 


通过以上配置,Web容器会自动加载/WEB-INF/applicationContext.xml初始化
ApplicationContext实例,如果需要指定配置文件位置,可通过context-param加以指定:
<context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>

配置完成之后,即可通过
WebApplicationContextUtils.getWebApplicationContext方法在Web应用中获取ApplicationContext引用。

如:ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext();
         LoginAction action=(LoginAction)ctx.getBean("action");

 

-------------------------------------------------------------------------------------------

spring为ApplicationContext提供有三种实现(举例)

 

         spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下:

   1. FileSystemXmlApplicationContext
           eg1. ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件
           eg2.
                   String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
                   ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加载
配置文件
           eg3.        
        ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");//根据具体路径加载文件

  2. ClassPathXmlApplicationContext
           eg1.  
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
           eg2.
                   String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
                   ApplicationContext ctx = new ClassPathXmlApplication(locations);
           注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。

分享到:
评论

相关推荐

    Spring 加载多个配置文件

    Spring 加载多个配置文件

    spring加载多个配置文件

    在spring中加载多个配置文件的方法,希望对你有用。

    Spring中如何加载多个配置文件.pdf

    本文详细介绍了Spring中如何加载多个配置文件

    spring如何加载配置多个配置文件

    主要介绍了spring如何加载配置多个配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    multi-properties:SpringBoot引入多个配置文件

    SpringBoot引入多个配置文件 1.自定义EnvironmentPostProcessor的实现类,在回调中加载自定义的配置文件 2.在META-INF/spring.factories中添加配置: org.springframework.boot.env.EnvironmentPostProcessor=...

    spring配置文件详解--真的蛮详细

    Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。 下面列举的是一份比较完整的配置文件模板,...

    Spring加载配置和读取多个Properties文件的讲解

    今天小编就为大家分享一篇关于Spring加载配置和读取多个Properties文件的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    在Spring Boot中加载XML配置的完整步骤

    主要给大家介绍了关于在Spring Boot中加载XML配置的完整步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    尚硅谷]_佟刚_Spring IOC 容器中 Bean 的生命周期.pdf

    3、import用于导入其他配置文件的Bean定义,这是为了加载多个配置文件,当然也可以把这些配置文件构造为一个数组(new String[] {“config1.xml”, config2.xml})传给ApplicationContext实现进行加载多个配置文件,...

    17-IoC配置-import导入配置文件

    Spring容器加载多个配置文件(了解) new classPathxmlApplicationcontext ( "config1.xml " , "config2.xml" ); Spring容器中的bean定义冲突问题 同id的bean,后定义的覆盖先定义的 导入配置文件可以理解为...

    springboot 的配置文件加载顺序

    springboot 的配置文件加载顺序 https://blog.csdn.net/dearmite/article/details/131045230 本系列校训 用免费公开视频,卷飞培训班哈人!打死不报班,赚钱靠狠干! 只要自己有电脑,前后项目都能搞!N年苦学无人...

    spring管理struts的action的代码

    spring管理struts的action的代码 从类路径下加载spring的配置文件, 多个配置文件可以用逗号和空格区分 * classpath: 关键字特指类路径下加载

    高级开发spring面试题和答案.pdf

    SPI 机制(Java SPI 实际上是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制), 很多地方有用到: AOP Spring的AOP的底层实现原理; 为什么jdk动态代理是必须是接口 两种动态代理的区别 AOP实现方式:...

    Spring.net框架

    我们的Factory就是利用这种方式根据配置文件动态加载程序集,动态创建对象并设置属性的。有了这个Factory,MainApp中的内容就很简单了: using System; namespace IocInCSharp { public class MainApp { public ...

    SpringBoot的配置文件

    SpringBoot配置文件类型 SpringBoot配置文件类型和作用: SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用 application.properties或者application.yml...

    spring security 参考手册中文版

    5.7多个HttpSecurity 41 5.8方法安全性 43 5.8.1 EnableGlobalMethodSecurity 43 5.8.2 GlobalMethodSecurityConfiguration 44 5.9后处理配置的对象 45 5.10自定义DSL 46 6.安全命名空间配置 47 6.1简介 47 6.1.1...

    spring-boot-camel-xml:一个快速入门,展示了如何将Spring Boot和camel与XML DSL以及Kubernetes或OpenShift一起使用

    本示例演示了如何通过Spring XML配置文件在Spring Boot中配置骆驼路线。 该应用程序利用Spring 批注通过类路径上的src / main / resources / spring / camel-context.xml文件加载Camel Context定义。 重要的 该...

    Spring Boot中文文档.rar

    多个档案的YAML文件 24.7.4.YAML缺点 24.8.类型安全的配置属性 24.8.1.第三方配置 24.8.2.轻松绑定 24.8.3.合并复杂类型 24.8.4.属性转换 转换持续时间 转换数据大小 24.8.5.@...

    Spring.3.x企业应用开发实战(完整版).part2

    4.7 整合多个配置文件 4.8 Bean作用域 4.8.1 singleton作用域 4.8.2 prototype作用域 4.8.3 Web应用环境相关的Bean作用域 4.8.4 作用域依赖问题 4.9 FactoryBean 4.10 基于注解的配置 4.10.1 使用注解定义Bean ...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

Global site tag (gtag.js) - Google Analytics