当前位置:网站首页>@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);
}
}边栏推荐
- Yolov5 network structure + code + application details | CSDN creation punch in
- 1106 lowest price in supply chain (25 points)
- Learn libcef together -- set cookies for your browser
- XML Configuration File
- SSM framework integration
- (完美解决)matplotlib图例(legend)如何自由设置其位置
- Go practice -- design patterns in golang's singleton
- 1115 counting nodes in a BST (30 points)
- 1111 online map (30 points)
- [set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
猜你喜欢

Online VR model display - 3D visual display solution
![[research materials] 2021 China's game industry brand report - Download attached](/img/b7/a377b0b7c742078e2feb28ebfbca62.jpg)
[research materials] 2021 China's game industry brand report - Download attached

Differences among bio, NiO and AIO

Burp suite plug-in based on actual combat uses tips

Oracle SQL table data loss

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)

Actual combat 8051 drives 8-bit nixie tube

Source insight garbled code solution
![[practical project] autonomous web server](/img/99/892e600b7203c63bad02adb683c8f2.png)
[practical project] autonomous web server

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
随机推荐
appium1.22.x 版本后的 appium inspector 需单独安装
112 stucked keyboard (20 points)
leetcode435. Non overlapping interval
Promise
XML配置文件
Wechat applet distance and map
1107 social clusters (30 points)
Go practice -- gorilla / websocket used by gorilla web Toolkit
appium1.22.x 版本後的 appium inspector 需單獨安裝
Kept hot standby and haproxy
[develop wechat applet local storage with uni app]
Silent authorization login and registration of wechat applet
50 practical applications of R language (36) - data visualization from basic to advanced
最大连续子段和(动态规划,递归,递推)
Redis Introduction et explication des types de données
leetcode406. Rebuild the queue based on height
Webapidom get page elements
Go language interface learning notes Continued
Go practice -- use JWT (JSON web token) in golang
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
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