Spring初始化ApplicationContext为null
1. ApplicationContextAware初始化
通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。
我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
使用方法如下:
1.实现ApplicationContextAware接口:
package com.bis.majian.practice.module.spring.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextHelper implements ApplicationContextAware {
private static ApplicationContext context = null; @Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
}
public static Object getBean(String name){
return context.getBean(name);
} }
2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">
<bean id="springContextHelper" class="com.bis.majian.practice.module.spring.util.SpringContextHelper"></bean>
<context:component-scan base-package="com.bis.majian.practice.module.*" />
</beans>
3.在web项目中的web.xml中配置加载Spring容器的Listener:
<!-- 初始化Spring容器,让Spring容器随Web应用的启动而自动启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4.在项目中即可通过这个SpringContextHelper调用getBean()方法得到Spring容器中的对象了。
2. ApplicationContext加载机制
1.加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet
这两者在功能上完全等同,只是一个是基于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>classpath:applicationContext-bean.xml,classpath:spring-jamon.xml </param-value>
</context-param>
2.Spring提供ApplicationContext多种实现机制
简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");
如果是两个以上:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});
或者用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
spring为ApplicationContext提供的3种实现分别为:
ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。
其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下:
(1)FileSystemXmlApplicationContext
//加载单个配置文件
ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"}; //加载多个配置文件
ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //根据具体路径加载文件
ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");
注: 这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
(2)ClassPathXmlApplicationContext
//加载单个配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//配置完成之后,即可通过ContextLoader工具类获取WebApplicationContext
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
LoginAction action=(LoginAction) wac.getBean("action");
(3) XmlWebApplicationContext
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
注:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
3.如何获取ApplicationContext
(1)继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
(2)继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext (3)实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存 ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。
实现方法:
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
applicationContext = arg0;
}
获取bean:
ITaskService bean = (ITaskService)applicationContext.getBean(taskServiceName);
3. Spring获取WebApplicationContext为null解决方案
在web.xml中配置Spring,配置如下
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
在Servlet中通过WebApplicationContextUtils.getWebApplicationContext(getServletContext())获取WebApplicationContext对象为null。这是由于除了配置DispatcherServlet,还需要配置ContextLoaderServlet,否则无法获取WebApplicationContext。配置方法如下,在web.xml中加入
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
案例2:
查找原因:xml文件的加载顺序不正确,ServiceBeanUtils还没有加载, ApplicationContext还没有初始化,而服务启动时就有个类通过调用ApplicationContext去取bean进行初始化了
解决方案:先加载ServiceBeanUtils类,去初始化ApplicationContext,然后再加载要调用ApplicationContext的类去初始化此类
4.ApplicationContext初始化方式
1. 在独立应用程序中,获取ApplicationContext:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();//释放资源
2. 在web环境中,获取ApplicationContext:
A)ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
B)String contextpath = "org.springframework.web.context.WebApplicationContext.ROOT";
WebApplicationContext context = request.getSession().getServletContext().getAttribute(contextpath);
5.常用获取spring 中bean的方式总结
方法一:在初始化时保存ApplicationContext对象
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
方法五:实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。
使用的时候一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件application-context.xml文件中进行配置。否则获取的ApplicationContext对象将为null。
Spring初始化ApplicationContext为null的更多相关文章
- Spring初始化ApplicationContext线程托管实际运用架构构思
今天我分享一个技术点,利用Spring初始化+线程接管进行程序启动后保持会话状态. 先来一段@test单元测试注解,后台开发的很熟悉,这是测试局部代码用的: @RunWith(SpringJUnit4 ...
- Spring中ApplicationContext加载机制和配置初始化
Spring中ApplicationContext加载机制. 加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet. ...
- Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式
转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236 Spring获取ApplicationContex ...
- 怎么获取Spring的ApplicationContext
在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...
- Spring中ApplicationContext加载机制
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp33 加载器目前有两种选择:ContextLoaderListener和Co ...
- 获取spring的ApplicationContext几种方式【转】
转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...
- Spring:ApplicationContext (2)
在使用Spring时,通常会配置一个applictioncontext.xml 来指定ApplicationContext的相关信息. 当使用SpringMVC时,还会再另外指定一个[server-n ...
- Spring中ApplicationContext对事件的支持
Spring中ApplicationContext对事件的支持 ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...
- Spring容器-ApplicationContext的单例设计
Spring容器-ApplicationContext的单例设计 每次通过new创建一个ApplicationContext容器,都会执行refresh方法,看源代码了解到这个refresh方法会 ...
随机推荐
- LVS-DR模式(原理图详解)
标签(空格分隔): linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 前言 LVS一共四种工作模式.其中,DR模式是比较常用的模式之一,配置较麻烦,这里 ...
- javacript 组合使用构造函数模式和原型模式
构造函数模式创建对象 基本方法 function Person(name,age){ this.name=name; this.age=age; this.sayName=function(){ al ...
- IT 人士如何避免中年危机?
今天咱们不谈技术,来聊点别的. 这也可能是比学习具体技术更重要的话题 - 投资. 我把投资分成两类: 投资股票.期货.现货.黄金这类常见投资品种. 投资自己.比如看书.学习.参加培训.当然<每天 ...
- oralce plsql案例练习
以下plsql程序用的scott用户的dept,emp表. 案例1 --查询80,81,82,87年员工入职人数 set serveroutput on declare cursor cemp is ...
- SQL Server比较2table字段的差异
由于项目前后用了2个数据库,需要统计数据库结构的变化,需要统计每个表的变化,由于人工核对挺浪费时间,就写了一点代码: 1.统计表的字段数量(查询表有多少列): select count(name) ...
- 14_Python字符串操作方法总结
字符串方法总结 #s = '**i love you\n\t' 测试s.strip()使用的字符串 s = 'i love you' #1.首字符大写,其余字符小写 print(s.capitaliz ...
- Storm 对 0.10.x 版 Kafka之commit offsets
由于 0.10.x 版 Kafka 与 0.8.x 版有很大的变化,这种变化对下游 Storm 有非常大的影响,0.10.x 版的 Kafka 不但增加了权限管理的功能,而且还将 simple 和 h ...
- jboss7.1.1相关error及解决办法
问题1: 报错提示: 公司的网站系统使用的中间是jboss7.1.1 Final,由于有些数据和服务测试环境不具备,免不了要在测试环境调试些jsp页面,发现从jboss社区里下载的jboss应用有个问 ...
- BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 [树状数组 离线 离散化]
传送门 刚才我还在郁闷网上怎么没人用$CDQ$分治做 突然发现根本没有时间序.... #include<iostream> #include<cstdio> #include& ...
- [LeetCode] 679. 24 Game(回溯法)
传送门 Description You have 4 cards each containing a number from 1 to 9. You need to judge whether the ...