当前位置:网站首页>@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);
}
}边栏推荐
- Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
- 微服务常见面试题
- Online VR model display - 3D visual display solution
- 最大连续子段和(动态规划,递归,递推)
- Pan details of deep learning
- Go language interface learning notes Continued
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- RT thread flow notes I startup, schedule, thread
- Redis 过期淘汰机制
- Oracle SQL table data loss
猜你喜欢

Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)

Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)

(perfect solution) how to set the position of Matplotlib legend freely

Actual combat 8051 drives 8-bit nixie tube

Shallow and first code

【实战项目】自主web服务器

2022-02-12 daily clock in: problem fine brush

(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)

(完美解决)matplotlib图例(legend)如何自由设置其位置

es7创建索引容易犯的错误
随机推荐
Redis expiration elimination mechanism
Technical analysis of qianyuantong multi card aggregation router
Common interview questions of microservice
"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
1106 lowest price in supply chain (25 points)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
谷歌 | 蛋白序列的深度嵌入和比对
(perfect solution) how to set the position of Matplotlib legend freely
112 stucked keyboard (20 points)
Yolov5 network structure + code + application details | CSDN creation punch in
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
Overview of basic knowledge of C language
study hard and make progress every day
Notes | numpy-07 Slice and index
最大连续子段和(动态规划,递归,递推)
Go practice -- use redis in golang (redis and go redis / redis)
Notes | numpy-11 Array operation
1115 counting nodes in a BST (30 points)
Chapter II program design of circular structure
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