当前位置:网站首页>@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);
}
}边栏推荐
- RT thread flow notes I startup, schedule, thread
- Redis 入门和数据类型讲解
- sql语句模糊查询遇到的问题
- Gbase8s unique index and non unique index
- ES7 easy mistakes in index creation
- 大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
- Kept hot standby and haproxy
- Redis breakdown penetration avalanche
- Pessimistic lock and optimistic lock of multithreading
- Go language interface learning notes
猜你喜欢

大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南

appium1.22.x 版本后的 appium inspector 需单独安装

Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in

RT thread flow notes I startup, schedule, thread

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

Online VR model display - 3D visual display solution
![[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached](/img/95/833f5ec20207ee5d7e6cdfa7208c5e.jpg)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
![[practical project] autonomous web server](/img/99/892e600b7203c63bad02adb683c8f2.png)
[practical project] autonomous web server
![[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)](/img/34/d195752992f8955bc2a41b4ce751db.jpg)
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)

Unity tool Luban learning notes 1
随机推荐
Introduction to deep learning (II) -- univariate linear regression
Why is go language particularly popular in China
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
Gbase8s unique index and non unique index
XML Configuration File
1119 pre- and post order traversals (30 points)
1110 complete binary tree (25 points)
Class loading mechanism (detailed explanation of the whole process)
Redis 入門和數據類型講解
[basic grammar] C language uses for loop to print Pentagram
1103 integer factorization (30 points)
1099 build a binary search tree (30 points)
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
1086 tree traversals again (25 points)
Redis 过期淘汰机制
Web APIs exclusivity
JS dynamic table creation
Three representations of signed numbers: original code, inverse code and complement code
Go practice -- use JWT (JSON web token) in golang
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
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