当前位置:网站首页>@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);
}
}边栏推荐
- "Hands on deep learning" pytorch edition Chapter II exercise
- Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
- Make your own dataset
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- 1111 online map (30 points)
- Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
- Web APIs exclusivity
- 1115 counting nodes in a BST (30 points)
- Source insight garbled code solution
- Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
猜你喜欢

"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

The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article

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

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

Principles of BTC cryptography

JS scope

Oracle SQL table data loss

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

Yolov5 network structure + code + application details | CSDN creation punch in
![[research materials] 2022q1 game preferred casual game distribution circular - Download attached](/img/13/5a67c5d08131745759fdc70a71cf0f.jpg)
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
随机推荐
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
Notes | numpy-11 Array operation
Notes | numpy-07 Slice and index
Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
Redis 入門和數據類型講解
Notes | numpy-08 Advanced index
Problems encountered in fuzzy query of SQL statements
Actual combat 8051 drives 8-bit nixie tube
1110 complete binary tree (25 points)
Pessimistic lock and optimistic lock of multithreading
Introduction to deep learning - definition Introduction (I)
[research materials] 2021 China's game industry brand report - Download attached
Introduction to rust Foundation (basic type)
(subplots用法)matplotlib如何绘制多个子图(轴域)
Why is go language particularly popular in China
编译GCC遇到的“pthread.h” not found问题
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