Spring读取配置文件的点点滴滴
2013-04-23 21:07
274 查看
Spring初始化容器.三种经常用到的实现:
一、ClassPathXmlApplicationContext:从类路径中加载。
二、FileSystemXmlApplicationContext:从文件系统加载。
三、XmlWebApplicationContext:从web系统中加载。
使用1、bean工厂:最简单的容器,提供了基础的依赖注入支持。创建各种类型的Bean.
BeanFactory factory = null; //声明
ClassPathResource resource = new ClassPathResource("spring.xml");//类路径
FileSystemResource fileSystemResource =
new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
//系统路径
//InputStreamResource res
//= new InputStreamResource(
//new FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));
factory= new XmlBeanFactory(resource);
//XmlBeanFactory(参数可以是resource或者fileSystemResource等
//但是不能是 res 原因可以查看:文档Part III. Core Technologies 6. Resources
//中6.2 The Resource interface 有关isOpen方法的说明);
factory = new ClassPathXmlApplicationContext("spring.xml");
//从类路径中加载
factory = new
FileSystemXmlApplicationContext("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"); //从文件系统加载
HelloService helloService =
factory.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
使用2、
应用上下文:建立在bean工厂基础之上,提供系统架构服务。ApplicationCotext,spring更加高级的容器。功能强大:
1.提供文本信息解析工具,包括对国际化支持。
2.提供载入文件资源的通用方法,如图片。
3.可以向注册为监听器的bean发送事件。
在很少的情况下,使用BeanFactory,如在移动设备。
ApplicationContext context = new
FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
context = new
ClassPathXmlApplicationContext("classpath:spring.xml");
HelloService helloService =
context.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
使用3、在web应用程序中
1、采用XMLWebApplication对象 初始化容器
XmlWebApplicationContext context = new XmlWebApplicationContext();
//默认的路径/WEB-INF/applicationContext.xml
//applicationContext.xml文件名称 可以任意起
//重新设置路径
//context.setConfigLocations(
// new String[] {"/WEB-INF/classes/applicationContext.xml"});
//设置ServletContext上下下文为web应用程序的上下文
context.setServletContext(getServletContext());
//刷新
context.refresh();
//根据id名称获取
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
//执行helloDao对象的方法
helloDao.sayHello();
因为XmlWebApplicationContext默认加载的文件和文件名称为:
/WEB-INF/applicationContext.xml
因此需要在WEB-INF下添加此配置文件。
2、利用 WebApplicationContextUtils工具类
//直接采用getWebApplicationContext(getServletContext()) 获取context对象
WebApplicationContext context=
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//context =
//WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
System.out.println(context);
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
helloDao.sayHello();
说明:
1、当采用getWebApplicationContext(getServletContext())获取context对象的时候,输出的context对象为null 所以在使用
context.getBean("helloDaoImpl", HelloDaoImpl.class);会出现空指针的异常
2、当采用getRequiredWebApplicationContext(getServletContext());获取context对象的时候 会出现如下bug
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
Bug解决:不关是1和2都是因为没有在web.xml文件中配置ContextLoaderListener,所以只需要在web.xml文件中加入以下代码中即可
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
一、ClassPathXmlApplicationContext:从类路径中加载。
二、FileSystemXmlApplicationContext:从文件系统加载。
三、XmlWebApplicationContext:从web系统中加载。
使用1、bean工厂:最简单的容器,提供了基础的依赖注入支持。创建各种类型的Bean.
BeanFactory factory = null; //声明
ClassPathResource resource = new ClassPathResource("spring.xml");//类路径
FileSystemResource fileSystemResource =
new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
//系统路径
//InputStreamResource res
//= new InputStreamResource(
//new FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));
factory= new XmlBeanFactory(resource);
//XmlBeanFactory(参数可以是resource或者fileSystemResource等
//但是不能是 res 原因可以查看:文档Part III. Core Technologies 6. Resources
//中6.2 The Resource interface 有关isOpen方法的说明);
factory = new ClassPathXmlApplicationContext("spring.xml");
//从类路径中加载
factory = new
FileSystemXmlApplicationContext("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"); //从文件系统加载
HelloService helloService =
factory.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
使用2、
应用上下文:建立在bean工厂基础之上,提供系统架构服务。ApplicationCotext,spring更加高级的容器。功能强大:
1.提供文本信息解析工具,包括对国际化支持。
2.提供载入文件资源的通用方法,如图片。
3.可以向注册为监听器的bean发送事件。
在很少的情况下,使用BeanFactory,如在移动设备。
ApplicationContext context = new
FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
context = new
ClassPathXmlApplicationContext("classpath:spring.xml");
HelloService helloService =
context.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
使用3、在web应用程序中
1、采用XMLWebApplication对象 初始化容器
XmlWebApplicationContext context = new XmlWebApplicationContext();
//默认的路径/WEB-INF/applicationContext.xml
//applicationContext.xml文件名称 可以任意起
//重新设置路径
//context.setConfigLocations(
// new String[] {"/WEB-INF/classes/applicationContext.xml"});
//设置ServletContext上下下文为web应用程序的上下文
context.setServletContext(getServletContext());
//刷新
context.refresh();
//根据id名称获取
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
//执行helloDao对象的方法
helloDao.sayHello();
因为XmlWebApplicationContext默认加载的文件和文件名称为:
/WEB-INF/applicationContext.xml
因此需要在WEB-INF下添加此配置文件。
2、利用 WebApplicationContextUtils工具类
//直接采用getWebApplicationContext(getServletContext()) 获取context对象
WebApplicationContext context=
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//context =
//WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
System.out.println(context);
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
helloDao.sayHello();
说明:
1、当采用getWebApplicationContext(getServletContext())获取context对象的时候,输出的context对象为null 所以在使用
context.getBean("helloDaoImpl", HelloDaoImpl.class);会出现空指针的异常
2、当采用getRequiredWebApplicationContext(getServletContext());获取context对象的时候 会出现如下bug
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
Bug解决:不关是1和2都是因为没有在web.xml文件中配置ContextLoaderListener,所以只需要在web.xml文件中加入以下代码中即可
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
相关文章推荐
- 读取Spring的配置文件applicationContext.xml的5种方法
- Spring中配置和读取多个Properties文件
- spring-boot 配置文件读取
- 读取Spring的配置文件applicationContext.xml几种方法
- Spring boot 读取配置文件(application.yml)中的属性值
- 读取Spring配置文件 main方法里面测试实现功能
- 详解Spring-boot中读取config配置文件的两种方式
- spring-配置文件的读取和解析
- springboot如何读取配置文件中的参数(例如:application-consts.properties) 又结合maven读取配置文件的顺序
- Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式
- SpringBoot自定义配置文件读取
- Spring Junit 读取WEB-INF下的配置文件
- Spring 读取配置文件(二)
- Spring中配置和读取多个Properties文件
- 关于Spring boot中读取属性配置文件出现中文乱码的问题的解决
- Spring读取二种绝对与类路径的配置文件备忘
- spring 配置文件属性设置默认值以及读取环境变量值
- Spring读取配置文件,获取bean的几种方式
- Spring读取配置文件,获取bean的几种方式