因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6种, xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:
XmlBeanFactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext
一: XmlBeanFactory 引用资源
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
二: ClassPathXmlApplicationContext 编译路径
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// src目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext(new String[] {"bean1.xml","bean2.xml"});
// src/conf 目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");
三: 用文件系统的路径
ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");
//使用了 classpath: 前缀,作为标志, 这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径
ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
四: XmlWebApplicationContext是专为Web工程定制的。
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
五: 使用BeanFactory
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;
六:Web 应用启动时加载多个配置文件
通过ContextLoaderListener 也可加载多个配置文件,在web.xml文件中利用
<context-pararn>元素来指定多个配置文件位置,其配置如下:
<context-param>
<!-- Context Configuration locations for Spring XML files -->
<param-name>contextConfigLocation</param-name>
<param-value>
./WEB-INF/**/Appserver-resources.xml,
classpath:config/aer/aerContext.xml,
classpath:org/codehaus/xfire/spring/xfire.xml,
./WEB-INF/**/*.spring.xml
</param-value>
</context-param>
这个方法加载配置文件的前提是已经知道配置文件在哪里,虽然可以利用“*”通配符,但灵活度有限。
分享到:
相关推荐
spring 重新动态加载数据库或xml中的bean,可以不用重启tomcat
主要给大家介绍了关于在Spring Boot中加载XML配置的完整步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
加载spring 文件,在web.xml中的配置
spring boot中配置mybatis xml资源文件热加载的方法以及相关文件
SpringBoot中自定义日志配置logback-spring.xml示例源码
spring boot中配置mybatis xml资源文件热加载的方法以及相关文件
下面小编就为大家带来一篇java实现Spring在XML配置java类的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件1
springmvc项目中如何加载jar包中的spring配置文件
spring整合springmvc、mybatis的jar包和配置文件,spring整合springmvc、mybatis的jar包和配置文件,spring整合springmvc、mybatis的jar包和配置文件
本示例演示了如何通过Spring XML配置文件在Spring Boot中配置骆驼路线。 该应用程序利用Spring 批注通过类路径上的src / main / resources / spring / camel-context.xml文件加载Camel Context定义。 重要的 该...
主要介绍了SpringBoot之logback-spring.xml不生效的解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
5,另一种配置springMVC的方式 6,自定义(扩展)mvc配置 7,解决@ResponseBody return String的中文乱码问题 8,配置静态资源映射ResourceHandlers 9,配置ViewControllers 10,配置filter 11,配置拦截器
加载 DispatcherServlet 时 Spring 会尝试读取配置文件 默认的配置文件位于 web.xml 相同的路径下 文件名与注册的 Servlet 名有关 Servlet注册名跟上 -servlet.xml 例如:上面的 Servlet 注册名为 ...
5,另一种配置springMVC的方式 6,自定义(扩展)mvc配置 7,解决@ResponseBody return String的中文乱码问题 8,配置静态资源映射ResourceHandlers 9,配置ViewControllers 10,配置filter 11,配置拦截器
spring基于xml配置加载bean 的demo 了解spring的反射机制
Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。 下面列举的是一份比较完整的配置文件模板,...
java servlet spring web filter listenr
web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库、spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者...