开发者社区> 问答> 正文

SpringContextHolder 获取不到applicationContext?报错

 

web.xml
 

<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/spring/applicationContext.xml/</param-value>
  </context-param>

 

applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
    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.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
         
    <bean id="springContextHolder" class="com.joethina.common.SpringContextHolder"/>
</beans>

SpringContextHolder.java

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class SpringContextHolder implements ApplicationContextAware{
 
    private static ApplicationContext applicationContext;
     
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }
     
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T)applicationContext.getBean(name);
    }
     
    public static <T> T getBean(Class<T> requireType) {
        return (T)applicationContext.getBean(requireType);
    }
     
    public static ApplicationContext getApplicationContext() {
        return SpringContextHolder.applicationContext;
    }
     
 
}

获取applicationContext的测试类

import org.springframework.context.ApplicationContext;
 
import com.joethina.common.SpringContextHolder;
 
public class Test1 {
 
    public static void main(String[] args) {
        //Object bean = SpringContextHolder.getBean("sss");
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        System.out.println(applicationContext);
    }
}

报错如下:

null
Exception in thread "main" java.lang.NullPointerException
    at com.joethina.common.SpringContextHolder.getBean(SpringContextHolder.java:18)
    at com.joethina.test.Test1.main(Test1.java:12)

各路大神帮忙看下,万分感谢。。

展开
收起
爱吃鱼的程序员 2020-06-07 17:25:12 1216 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>你测试的代码没有起ApplicationContext,你启动你的Servlet容器,就能获取到了。</p>
    
                    
    
                        <div class='ref'><h4>引用来自“yongk”的评论</h4><p>你测试的代码没有起ApplicationContext,你启动你的Servlet容器,就能获取到了。</p>
    
    
                        <div class='ref'><div class='ref'><h4>引用来自“yongk”的评论</h4><p>你测试的代码没有起ApplicationContext,你启动你的Servlet容器,就能获取到了。</p>
    
    
                        我觉得是加载顺序的问题,你看下setContext和getBean执行顺序就知道了 
                    
    
                        根本不是一个运行环境,我也是醉了。。。 
    
    2020-06-07 17:25:31
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载