当前位置:网站首页>@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);
}
}
边栏推荐
- How to connect the network: Chapter 1 CSDN creation punch in
- 请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
- 1115 counting nodes in a BST (30 points)
- Basic introduction of redis and explanation of eight types and transactions
- Promise
- Basic knowledge of reflection (detailed explanation)
- 聊聊如何利用p6spy进行sql监控
- Online VR model display - 3D visual display solution
- 2022-02-12 daily clock in: problem fine brush
- Introduction to rust Foundation (basic type)
猜你喜欢
穀歌 | 蛋白序列的深度嵌入和比對
[practical project] autonomous web server
cookie session jwt
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Actual combat 8051 drives 8-bit nixie tube
Why is go language particularly popular in China
Burp suite plug-in based on actual combat uses tips
Audio Focus Series: write a demo to understand audio focus and audiomananger
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
随机推荐
Explanation of several points needing attention in final (tested by the author)
Go practice -- design patterns in golang's singleton
1095 cars on campus (30 points)
Go practice -- use redis in golang (redis and go redis / redis)
Actual combat 8051 drives 8-bit nixie tube
1118 birds in forest (25 points)
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
最大连续子段和(动态规划,递归,递推)
study hard and make progress every day
XML Configuration File
Go practice -- closures in golang (anonymous functions, closures)
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
乾元通多卡聚合路由器的技术解析
在PyCharm中配置使用Anaconda环境
Best practices for setting up altaro VM backups
1110 complete binary tree (25 points)
Yolov5 input (II) | CSDN creative punch in
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
"Pthread.h" not found problem encountered in compiling GCC
Redis 入門和數據類型講解