当前位置:网站首页>@Autowired 导致空指针报错 解决方式
@Autowired 导致空指针报错 解决方式
2022-07-03 05:16:00 【YTIANYE】
报错
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
我的B类中,对A类型的变量加上了注解@Autowired,查到的解决方法大概有两种方式,通常可以由上下文获取类进行解决,于是我在B中由工具获取到A的类,并使用A的方法,还是空指针错误。
SpringBoot使用@Autowired注入静态变量报空指针异常_BBQ__XB的博客-CSDN博客
解决方法:
由于B类是在main 中以示例的方式声明的,那么由B的实例b去使用A的方法一定是错误的。
通过工具类在main中获取B的方式,就不会报错了。
Java变量的初始化顺序为:
静态变量或静态语句块–>实例变量或初始化语句块–>构造方法–>@Autowired
工具类
package main
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
/**
* 功能描述: 从Spring上下文中去获取到类,解决@Autowird注入空指针问题。
*
*/
@Component
@Slf4j
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (null == SpringUtil.context) {
SpringUtil.context = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return context;
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
ApplicationContext applicationContext = getApplicationContext();
log.info("SpringUtil getBean ApplicationContext is {}, class is {}", applicationContext, clazz.getName());
return applicationContext.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
ApplicationContext applicationContext = getApplicationContext();
log.info("SpringUtil getBean ApplicationContext is {}, name is:{}, class is {}", applicationContext, name,
clazz.getName());
return applicationContext.getBean(name, clazz);
}
}边栏推荐
- 1110 complete binary tree (25 points)
- (subplots用法)matplotlib如何绘制多个子图(轴域)
- 112 stucked keyboard (20 points)
- Wechat applet distance and map
- Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
- [set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
- (subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
- Overview of basic knowledge of C language
- Online VR model display - 3D visual display solution
- Common methods of JS array
猜你喜欢

穀歌 | 蛋白序列的深度嵌入和比對

【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)

音频焦点系列:手写一个demo理解音频焦点与AudioMananger

cookie session jwt

Make your own dataset

Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
![[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)](/img/c2/87358af6b2b2892a6eceb751b3b60c.jpg)
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution

Shuttle + alluxio accelerated memory shuffle take-off

Botu uses peek and poke for IO mapping
随机推荐
【实战项目】自主web服务器
112 stucked keyboard (20 points)
JQ style, element operation, effect, filtering method and transformation, event object
Go language interface learning notes
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
Technical analysis of qianyuantong multi card aggregation router
Kept hot standby and haproxy
Silent authorization login and registration of wechat applet
Notes | numpy-11 Array operation
[practical project] autonomous web server
Audio Focus Series: write a demo to understand audio focus and audiomananger
Pan details of deep learning
Go practice -- design patterns in golang's singleton
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
1086 tree traversals again (25 points)
1103 integer factorization (30 points)
Gbase8s unique index and non unique index
https://blog.csdn.net/bookssea/article/details/109196195?ops_request_misc=&request_id=&biz_id=102&utm_term=autowired%E6%B3%A8%E8%A7%A3%E6%8A%A5%E7%A9%BA%E6%8C%87%E9%92%88&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-109196195.nonecase&spm=1018.2226.3001.4187