ApplicationContextAware 上下文空
时间: 2023-10-17 19:01:23 浏览: 101
通过分析引用和引用中的内容可以得知,问题出现的原因是在实现类中定义的applicationContext未加关键字static。这意味着每个实例化的对象都会有一个自己的applicationContext,而不是共享同一个上下文。因此,在获取ApplicationContext时,由于没有正确设置静态的applicationContext字段,导致获取到的上下文为空。
解决这个问题的方法是将ApplicationContext定义为static关键字,这样就能够确保所有的实例化对象共享同一个上下文。修改的代码示例如下:
```
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
```
这样,当调用setApplicationContext方法时,正确的将上下文赋值给了静态的applicationContext字段,使得获取ApplicationContext时不再为空。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [ApplicationContextAware接口方式去获取上下文报NUllPointerException](https://blog.csdn.net/z956281507/article/details/77478570)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [异常解决——获取Spring上下文AppplicationContextAware时applicationContext为null](https://blog.csdn.net/shijing266/article/details/108834405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文