Spring加载配置文件的三种方式
来源:互联网 发布:服装设计绘图软件 编辑:程序博客网 时间:2024/04/30 04:30
1、把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式
2、采用在web.xml中配置ContextLoaderListenera或ContextLoaderServlet指定加载路径方式。
它们两个有着同样的功能,都实现在了org.springframework.web.context.ContextLoader类,都要定义contextConfigLocation参数。区别在于listener不能在Servlet 2.2兼容的容器中使用。自从Servelt 2.4规范,listener被要求在web应用启动后初始化。web.xml初始化的时候,listerner会检查contextConfigLocation参数。如果不存在的话,它将默认使用/WEB-INF/applicationContext.xml。如果它存在,它就会用预先定义的分隔符(逗号,分号和空格)分开分割字符串(<param-value></param-value),并将这些值作为应用上下文将要搜索的位置。
代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/daoContext.xml,
/WEB-INF/config/appContext1.xml,
/WEB-INF/config/appContext2.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 另一种是使用ContextLoaderServlet
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
3 通过ClassPathXmlApplicationContext或XmlWebApplicationContext代码动态加载
一:XmlBeanFactory 引用
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
二:ClassPathXmlApplicationContext 编译路径
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml"); // src目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml"); // src/conf 目录下的
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 );
- Spring加载配置文件的三种方式
- java加载配置文件的三种方式
- Spring加载配置文件的两种方式
- spring加载配置文件的几种方式
- spring加载配置文件的方式
- spring加载配置文件的方式
- spring 加载配置文件的方式
- Spring读取配置文件的三种方式
- Spring的ApplicationContext加载多个配置文件的三种方式
- Spring的ApplicationContext加载多个配置文件的三种方式
- Spring (一) 加载配置文件,实例化类, 三种实例化bean的方式
- spring 配置文件加载方式
- spring加载配置文件方式
- 几种加载Spring的配置文件的方式
- spring 中加载xml配置文件的4种方式
- Spring中加载xml配置文件的六种方式
- Spring中加载xml配置文件的六种方式
- Spring中加载XML配置文件的6种方式
- ios修改textField的placeholder的字体颜色、大小
- Qt4第三方库QJSON编译说明
- text-indent在ie6下input发生位移
- wpf 异步命令
- 法国的好的好的
- Spring加载配置文件的三种方式
- [LeetCode] Sort Colors
- POSIX 线程详解
- Centos6.5上安装xen及图形化界面管理
- UVa1647Computer Transformation
- IOS-DEMO6 LayerMask
- poj 3262 Protecting the Flowers 【贪心】
- hdu 4634 Swipe Bo
- XBOOT核心组件之KOBJ技术详解