spring applicationcontextaware null

来源:互联网 发布:淘宝店新手卖什么好 编辑:程序博客网 时间:2024/06/03 17:52

Spring初始化ApplicationContext为null

1. ApplicationContextAware初始化

通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。

我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。

使用方法如下:

1.实现ApplicationContextAware接口:

 2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:

 3.在web项目中的web.xml中配置加载Spring容器的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

或者

1
2
3
4
5
6
7
<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加以指定:  

1
2
3
4
<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实例(对象).可以用:  

1
2
3
4
5
6
7
8
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  

1
2
3
4
5
//加载单个配置文件
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  

(3) XmlWebApplicationContext

注:这种方式适合于采用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对象注入。 

  实现方法: 

3. Spring获取WebApplicationContext为null解决方案

在web.xml中配置Spring,配置如下

1
2
3
4
5
6
7
8
9
<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中加入

1
2
3
4
5
<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对象

1
2
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

方法二:通过Spring提供的工具类获取ApplicationContext对象

1
2
3
4
5
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。

 尤其是最后一句,一定要注意,使用的时候一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件application-context.xml文件中进行配置。否则获取的ApplicationContext对象将为null。

坚持不懈

我的问题就是最后一句话解决的!
原创粉丝点击