读取applicationContext.xml(spring配置文件)文件
来源:互联网 发布:福建乐游网络 编辑:程序博客网 时间:2024/04/28 18:51
读取applicationContext.xml(spring配置文件)文件:(通过编码加载)
方法一:
ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml");
方法二:
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
其他:
//得到BeanFactory
InputStream is = new FileInputStream("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
//通过BeanFactory获取Bean实例:
BeanName bean=(BeanName)factory.getBean("beanName");
对于WEB应用,spring提供了可配置的ApplicationContext加载机制:
加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet。这两者在功能上完全
等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。
配置非常简单,在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.xml(spring配置文件)文件
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring配置文件详解 - applicationContext.xml文件路径
- spring配置文件applicationContext.xml
- spring配置文件 applicationContext.xml
- Spring配置文件-applicationContext.xml
- 读取Spring的配置文件applicationContext.xml几种方法
- 模拟spring的注入和读取配置文件(ApplicationContext.xml)
- 模拟spring的注入和读取配置文件(ApplicationContext.xml)
- 读取Spring的配置文件applicationContext.xml的5种方法
- 读取Spring配置文件ApplicationContext.xml的5种方法
- 读取Spring的配置文件applicationContext.xml的5种方法
- applicationContext.xml配置文件的读取
- Spring 读取配置文件得到ApplicationContext
- spring读取配置文件得到ApplicationContext
- grep用法详解:grep与正则表达式
- StringUtils(org.apche.lang)常用
- 自绘标准CTreeCtrl的实现(NM_CUSTORMDRAW方法)
- Spring的StringUtils工具类
- 一道面试题
- 读取applicationContext.xml(spring配置文件)文件
- struts-config.xml
- Android开发之旅:HelloWorld项目的目录结构
- 恶意伪IE图标清除方法
- Windows 下 Apache HttpServer + Tomcat 的整合
- VC-获取文件夹中的指定类型的所有文件名和文件大小
- 缓解眼疲劳电脑窗口设置
- 何为DMA
- 集成displaytag时log4j冲突