错误详细信息:
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com....websocket.WebSocketServer]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.cloudwise.isop.messagecenter.config.websocket.WebSocketServer]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1295)
... 25 more
Caused by: java.lang.NullPointerException
at com.cloudwise.isop.messagecenter.config.websocket.WebSocketServer.<init>(WebSocketServer.java:33)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)
关键信息:
com.cloudwise.isop.messagecenter.config.websocket.WebSocketServer.<init>(WebSocketServer.java:33)
这块代码:
private final RedisUtil redisUtil = SpringContextUtil.getApplicationContext().getBean(RedisUtil.class);
很明显 SpringContextUtil.getApplicationContext() 报空指针
追踪代码到 SpringContextUtil 类
@Component
public class SpringContextUtil implements ApplicationContextAware {
/***
* Spring应用上下文环境
*/
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过class获取Bean
**/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
}
推测:调用 getApplicationContext() 时, applicationContext 未初始化
打上日志发现:
getApplicationContext() 时, setApplicationContext() 尚未触发,结果导致 applicationContext 为 null
查询 ApplicationContextAware 类的使用方法
-- 当springboot 容器启动成功后会 setApplicationContext()
但我们调用 getApplicationContext() 在 Bean 初始化的时候,显然此时 setApplicationContext() 尚未执行, 不能在初始化的时候调用 getApplicationContext()
网友评论