In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to solve the @ Autowired injection null pointer problem, the content is detailed and easy to understand, the operation is simple and quick, and has a certain reference value. I believe you will gain something after reading this @ Autowired injection null pointer problem. Let's take a look.
I wrote the following code to extract import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * @ author BestQiang * / @ Component@ConfigurationProperties (prefix = "thread-pool") public class ThreadPool {private int corePoolSize; private int maximumPoolSize; private long keepAliveTime; private int capacity; public int getCorePoolSize () {return corePoolSize;} public void setCorePoolSize (int corePoolSize) {this.corePoolSize = corePoolSize } public int getMaximumPoolSize () {return maximumPoolSize;} public void setMaximumPoolSize (int maximumPoolSize) {this.maximumPoolSize = maximumPoolSize;} public long getKeepAliveTime () {return keepAliveTime;} public void setKeepAliveTime (long keepAliveTime) {this.keepAliveTime = keepAliveTime;} public int getCapacity () {return capacity;} public void setCapacity (int capacity) {this.capacity = capacity }} package cn.bestqiang.util;import cn.bestqiang.pojo.ThreadPool;import com.google.common.util.concurrent.ThreadFactoryBuilder;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.util.concurrent.*;/** * @ author Yaqiang Chen * / @ Componentpublic class MyThreadUtils {@ Autowired ThreadPool threadPool1 Private ExecutorService threadPool = new ThreadPoolExecutor (threadPool1.getCorePoolSize (), threadPool1.getMaximumPoolSize (), threadPool1.getKeepAliveTime (), TimeUnit.SECONDS, new LinkedBlockingDeque (threadPool1.getCapacity ()), namedThreadFactory, new ThreadPoolExecutor.DiscardPolicy ()) Private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder () .setNameFormat ("pool-%d") .build (); public void execute (Runnable runnable) {threadPool.submit (runnable);}}
The configuration in the yml file is as follows:
Thread-pool: core-pool-size: 5 maximum-pool-size: 20 keep-alive-time: 1 capacity: 1024
I thought there should be no problem, but I made a mistake:
Org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myThreadUtils' defined in fileXXXXXXXXXX (omitted) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.itcast.util.MyThreadUtils]: Constructor threw exception; nested exception is java.lang.NullPointerExceptionCaused by: java.lang.NullPointerException: null
The problem is solved easily.
That's the answer. All Spring's @ Autowired annotations are after the constructor, and if an object is declared like the following code (private XXX = new XXX () is declared directly in the class), the member variable is initialized before the constructor and can even be used as an argument to the constructor.
That is, member variable initialization-> Constructor-> @ Autowired
Therefore, at this time, if the object initialized with @ Autowired annotation is called when the member variable is initialized, it is bound to report a null pointer exception.
The truth has come out. What if it's solved? Just let the member variable threadPool of the code I wrote above be executed after @ Autowired.
To solve this problem, you first need to know how @ Autowired works:
The class AutowiredAnnotationBeanPostProcessor
In fact, seeing this inheritance structure, I already have a solution in mind. Specific details of why, when the work of 997 is over (helpless), I will explain the configuration of Spring notes in detail in the follow-up blog, and then I will talk about the life cycle of Bean.
The inherited BeanFactoryAware is executed only after the property assignment is completed, after the constructor is executed, and before other lifecycles, and the @ Autowired annotation relies on this principle for automatic injection. To solve this problem, it is easy to put the member variables to be assigned into other lifecycles.
Here are two methods: first, @ PostConstruct@PostConstructpublic void init () of JSR250 {/ / the assignment to be performed here} the second is the InitializingBean of Spring (define initialization logic)
Just inherit the interface implementation method, and put the complete usage directly.
/ * @ author Yaqiang Chen * / @ Componentpublic class MyThreadUtils implements InitializingBean {@ Autowired ThreadPool threadPool1; private ExecutorService threadPool; private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder () .setNameFormat ("pool-%d") .build (); public void execute (Runnable runnable) {threadPool.submit (runnable) @ Override public void afterPropertiesSet () throws Exception {threadPool = new ThreadPoolExecutor (threadPool1.getCorePoolSize (), threadPool1.getMaximumPoolSize (), threadPool1.getKeepAliveTime (), TimeUnit.SECONDS, new LinkedBlockingDeque (threadPool1.getCapacity ()), namedThreadFactory, new ThreadPoolExecutor.DiscardPolicy ()) }} this is the end of the article on "how to solve the @ Autowired injection null pointer problem". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to solve the @ Autowired injection null pointer problem". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.