java - ApplicationContextAware方式获取上下文,但是最终却报错:NullPointerException?
ringa_lee
ringa_lee 2017-04-18 09:45:34
0
6
616

1.今天想使用ApplicationContextAware接口方式去获取上下文环境,从而去获取bean,自己照着例子去写了一遍,但是最后还是报错,找不到这个bean,貌似实现了ApplicationContextAware的这个类根本就没有被Spring初始化的时候初始化执行setApplicationContext()方法(我已经在配置文件配置了这个bean),下面是我的代码:
2.工程代码
(1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>spring-test</display-name>
  <!-- spring config begin -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath*:config/spring/local/appcontext-*.xml
    </param-value>
  </context-param>
  <!-- end -->

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

(2).spring配置文件,我用的是idea,配置文件在config/spring/local下,Resource下面

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                            "> 
    <bean id="springTest" class="com.fcc.spring.test.SpringTest" lazy-init="false" /><!-- 谷歌搜索查到加lazy-init属性,但也没用 -->

    <bean id="helloWorld" class="com.fcc.spring.test.HelloWorld">
        <property name="message" value="Hello World!" />
    </bean>
</beans>

(3).SpringTest.java

package com.fcc.spring.test;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;

public class SpringTest implements ApplicationContextAware{

    private static ApplicationContext applicationContext;


    public static void main(String[] args){
        HelloWorld obj = (HelloWorld)SpringTest.getBean("helloWorld");
        System.out.println("obj = " + obj);
        System.out.println("The message value is " + obj.getMessage());
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("applicationContext before : " + applicationContext);
        SpringTest.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
    public static Object getBean(String name) throws BeansException{
        System.out.println("applicationContext = " + applicationContext);
        return applicationContext.getBean(name);
    }
}

HelloWorld的代码我就不贴了,就是一个简单的JavaBean,有个message:String属性
我理解的初始化顺序是这样的:
启动tomcat容器,加载web.xml,建立整个容器(Servlet容器,这里是tomcat吧)的上下文,ServletContext,这时web.xml有个监听器,就是ContextLoaderListener,监听到这个事件,就会去扫描spring配置文件,默认是applicationContext.xml文件(classpath,idea是Resource下),如果自定义,就应该如web.xml中的<context-param>标签那般配置,扫描这个指定的Spring配置文件,就会将文件中相应的bean加载,其中实现了ApplicationContextAware的bean类会去执行setApplicationContext()方法,将上下文自动初始化,但我这里并没有,根本就没有去执行这个方法,不知道配置哪里错了?

ringa_lee
ringa_lee

ringa_lee

reply all(6)
迷茫

It seems that I have found the problem. I am really stupid, really stupid. . .
It is said that the web.xml file will be started only after the tomcat container is started, but what I wrote is a controller program and there is no tomcat at all. . .

黄舟

What does it have to do with web containers? There is more than one way to load a spring container.

Ty80

ioc container is not initialized, instantiate an ApplicationContext first

伊谢尔伦

@青楼guilty ApplicationContextAware has the getBean() method. Why do you have to implement it? Write your own getBean() method. Can't you just use its method directly? Don't understand. So much code, doesn't it mean that spring obtains java beans through application context?

PHPzhong

SpringTest(实现ApplicationContextAware的类)必须要加入spring bean去管理,可以用Compnent注解或在xml文件去配。
不然spring不会处理这个,这也是applicationContextnull reasons.

小葫芦

I also encountered the same problem a few days ago. In my case, it was in a class instantiated through reflection and an instance managed by spring. I also wrote a class like this to get the spring instance, but I couldn't inject it at first. , then I just added lazy-init="false". I didn't need to add it when I used spring3. This time I used spring4. I hope it will be helpful to you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!