applicationContext=null 错误总结
来源:互联网 发布:安徽网络作家协会申请 编辑:程序博客网 时间:2024/06/10 22:59
Spring版本:
Spring 4.2.5
问题:
private static DictMapper dictDao = SpringContextHolder.getBean(DictMapper.class);通过SpringContextHolder获取上下文,applicationContext总是为null
问题分析:
1.SpringContextHolder采用配置文件的方式加载
<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 --> <bean id="springContextHolder" class="com.xxx.common.utils.SpringContextHolder" />
2.web.xml 已配置
<listener><description>spring监听器</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring mvc servlet --><servlet><description>spring mvc servlet</description><servlet-name>springMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><description>spring mvc 配置文件</description><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet>
3.项目启动时,为执行初始化 set方法
//实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; }
问题解决方法:
参考文档:ApplicationContextAware方式获取上下文,但是最终却报错:NullPointerException?
在Spring配置文件中配置错误,修改为:
<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 --> <bean id="springContextHolder" class="com.xxx.common.utils.SpringContextHolder" lazy-init="false"/>
至此,重启项目 ,执行set方法,项目正常!
---------------------------------------分割线-------------------------------------------------------
SpringContextHolder类:
import java.util.Map;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; /** * *以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. * @author zhuh */public class SpringContextHolder implements ApplicationContextAware{ private static ApplicationContext applicationContext; //实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } //取得存储在静态变量中的ApplicationContext. public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. @SuppressWarnings("unchecked") public static <T> T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. //如果有多个Bean符合Class, 取出第一个. @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz) { checkApplicationContext(); @SuppressWarnings("rawtypes") Map beanMaps = applicationContext.getBeansOfType(clazz); if (beanMaps!=null && !beanMaps.isEmpty()) { return (T) beanMaps.values().iterator().next(); } else{ return null; } } private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } } }
阅读全文
0 0
- applicationContext=null 错误总结
- spring ApplicationContext 使用总结
- spring学习错误总结:spring配置applicationContext自动加载配置 之404 cannot find resource
- NULL总结
- null总结
- String[] arrayIP= null;的典型错误
- assert(pMap != NULL)错误的解决办法
- assert(pMap != NULL)错误的解决办法
- hbase数据库错误总结 ——ERROR: Can't get master address from ZooKeeper; znode data == null
- null Nested exception: null 错误
- Spring配置文件总结-applicationContext.xml
- ApplicationContext hw = new ClassPathXmlApplicationContext("ApplicationContext.xml");
- 数据库中的NULL错误
- 'NULL' undeclared错误
- : Invalid protocol: null错误
- findViewById返回Null 错误
- Spring错误笔记--Failed to load ApplicationContext
- 求助!!!!Failed to load ApplicationContext错误
- 国建融科合创_企业初期BIM投入是战略,还是应付
- PowerDesigner之PDM(物理概念模型)
- 虚拟机与VMware
- Help Hanzo 素数打表进阶(大数据区间打表)
- 事件
- applicationContext=null 错误总结
- WEB后台--基于Token的WEB后台登录认证机制(并讲解其他认证机制以及cookie和session机制)
- step7 professional v11 sp2 sp1详细安装方法
- 百度地图API申请问题
- C++里的sort()函数的具体用法
- ImageIO: PNG invalid PNG file: iDOT doesn't point to valid IDAT chunk
- 信息论实验-信道容量迭代算法(C++实现)
- mysql导入导出文件
- linux系统调用