当前位置:网站首页>@Solutions to null pointer error caused by Autowired
@Solutions to null pointer error caused by Autowired
2022-07-03 05:18:00 【YTIANYE】
Report errors
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
my B Class , Yes A Type variables are annotated @Autowired, There are probably two solutions found , Usually, it can be solved by the context acquisition class , So I am B From the tool A Class , And use A Methods , Or null pointer error .
resolvent :
because B Class is in main Declared as an example in , So by the B Example b To use the A Your method must be wrong .
Through the tool class in main In order to get B The way , It won't be wrong .
Java The initialization order of variables is :
Static variable or static statement block –> Instance variable or initialization statement block –> Construction method –>@Autowired
Tool class
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;
/**
* Function description : from Spring Get the class in the context , solve @Autowird Injection null pointer problem .
*
*/
@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);
}
}边栏推荐
- Force GCC to compile 32-bit programs on 64 bit platform
- (subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
- ROS Compilation Principle
- Installing altaro VM backup
- Go practice - gorilla / handlers used by gorilla web Toolkit
- 微服务常见面试题
- Three representations of signed numbers: original code, inverse code and complement code
- 1111 online map (30 points)
- 动态规划——相关概念,(数塔问题)
- Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
猜你喜欢
![[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology](/img/f1/d0dc4dc3fe49a2d2cd9e452a0ce31e.jpg)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology

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

How to connect the network: Chapter 1 CSDN creation punch in

Actual combat 8051 drives 8-bit nixie tube

Yolov5 model construction source code details | CSDN creation punch in

About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller

Three representations of signed numbers: original code, inverse code and complement code

Burp suite plug-in based on actual combat uses tips

微服务常见面试题

Celebrate the new year together
随机推荐
Gbase8s unique index and non unique index
Introduction to deep learning - definition Introduction (I)
Yolov5 input (II) | CSDN creative punch in
Learn libcef together -- set cookies for your browser
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
C language program ideas and several commonly used filters
appium1.22.x 版本後的 appium inspector 需單獨安裝
Ueditor, FCKeditor, kindeditor editor vulnerability
JQ style, element operation, effect, filtering method and transformation, event object
微服务常见面试题
1111 online map (30 points)
Introduction to rust Foundation (basic type)
Go practice -- use JWT (JSON web token) in golang
穀歌 | 蛋白序列的深度嵌入和比對
Common methods of JS array
[basic grammar] Snake game written in C language
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
聊聊如何利用p6spy进行sql监控
Unity tool Luban learning notes 1
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