当前位置:网站首页>@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);
}
}边栏推荐
- Yolov5 network structure + code + application details | CSDN creation punch in
- Making coco datasets
- leetcode452. Detonate the balloon with the minimum number of arrows
- Web APIs exclusivity
- Pytorch through load_ state_ Dict load weight
- Actual combat 8051 drives 8-bit nixie tube
- Intégration profonde et alignement des séquences de protéines Google
- Ueditor, FCKeditor, kindeditor editor vulnerability
- appium1.22.x 版本后的 appium inspector 需单独安装
- Objects. Requirenonnull method description
猜你喜欢

"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released

Audio Focus Series: write a demo to understand audio focus and audiomananger

Web APIs exclusivity

Class loading mechanism (detailed explanation of the whole process)

音频焦点系列:手写一个demo理解音频焦点与AudioMananger

Pan details of deep learning

Why is go language particularly popular in China

DEX net 2.0 for crawl detection

Shallow and first code

Gan network thought
随机推荐
1107 social clusters (30 points)
Messy change of mouse style in win system
Force GCC to compile 32-bit programs on 64 bit platform
Go language interface learning notes
Pessimistic lock and optimistic lock of multithreading
"Hands on deep learning" pytorch edition Chapter II exercise
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
Disassembly and installation of Lenovo r7000 graphics card
Burp suite plug-in based on actual combat uses tips
Redis 入門和數據類型講解
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
DEX net 2.0 for crawl detection
Go practice -- closures in golang (anonymous functions, closures)
Yolov5 input (II) | CSDN creative punch in
编译GCC遇到的“pthread.h” not found问题
Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
Botu uses peek and poke for IO mapping
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
ES7 easy mistakes in index creation
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
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