当前位置:网站首页>@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);
}
}边栏推荐
- 谷歌 | 蛋白序列的深度嵌入和比对
- Go practice -- closures in golang (anonymous functions, closures)
- Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
- Principles of BTC cryptography
- Technical analysis of qianyuantong multi card aggregation router
- Burp suite plug-in based on actual combat uses tips
- My first Smartphone
- 6.23星期四库作业
- Force GCC to compile 32-bit programs on 64 bit platform
- 2022-02-11 daily clock in: problem fine brush
猜你喜欢

"Hands on deep learning" pytorch edition Chapter II exercise

Win10 install pytullet and test

Class loading mechanism (detailed explanation of the whole process)

Ueditor, FCKeditor, kindeditor editor vulnerability

(subplots用法)matplotlib如何绘制多个子图(轴域)

leetcode452. Detonate the balloon with the minimum number of arrows

How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in

微服务常见面试题

2022-02-11 daily clock in: problem fine brush

Audio Focus Series: write a demo to understand audio focus and audiomananger
随机推荐
Introduction to deep learning - definition Introduction (I)
JS string and array methods
Webapidom get page elements
Burp suite plug-in based on actual combat uses tips
Yolov5 input (II) | CSDN creative punch in
Basic introduction of redis and explanation of eight types and transactions
1106 lowest price in supply chain (25 points)
SimpleITK学习笔记
获取并监控远程服务器日志
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
50 practical applications of R language (36) - data visualization from basic to advanced
Redis Introduction et explication des types de données
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
leetcode860. Lemonade change
1115 counting nodes in a BST (30 points)
Yolov5 network structure + code + application details | CSDN creation punch in
Robot capture experiment demonstration video
Make your own dataset
Redis 入门和数据类型讲解
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
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