当前位置:网站首页>@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);
}
}
边栏推荐
- (perfect solution) how to set the position of Matplotlib legend freely
- Gbase8s unique index and non unique index
- JS string and array methods
- Redis 击穿穿透雪崩
- Redis Introduction et explication des types de données
- Promise
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- 联想R7000显卡的拆卸与安装
- 动态规划——相关概念,(数塔问题)
- 获取并监控远程服务器日志
猜你喜欢
Class loading mechanism (detailed explanation of the whole process)
Gbase8s composite index (I)
leetcode435. Non overlapping interval
XML Configuration File
Unity tool Luban learning notes 1
(subplots用法)matplotlib如何绘制多个子图(轴域)
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
Deploy crawl detection network using tensorrt (I)
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Celebrate the new year together
随机推荐
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
1099 build a binary search tree (30 points)
Redis Introduction et explication des types de données
Redis expiration elimination mechanism
Force GCC to compile 32-bit programs on 64 bit platform
Go practice -- use redis in golang (redis and go redis / redis)
Common methods of JS array
2022-02-11 daily clock in: problem fine brush
How to connect the network: Chapter 1 CSDN creation punch in
appium1.22.x 版本後的 appium inspector 需單獨安裝
Burp suite plug-in based on actual combat uses tips
【实战项目】自主web服务器
Make your own dataset
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
乾元通多卡聚合路由器的技术解析
Celebrate the new year together
ES7 easy mistakes in index creation
Class loading mechanism (detailed explanation of the whole process)
XML Configuration File
Introduction to rust Foundation (basic type)