解决@Autowired注入空指针问题(利用Bean的生命周期)
作者:Hermione Granger
今天做项目的时候遇到一个问题,需要将线程池的参数抽取到yml文件里进行设置。这不是so easy吗?
我就写出了下面这样的代码进行抽取
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 */ @Component public class MyThreadUtils { @Autowired ThreadPool threadPool1; private ExecutorService threadPool = new ThreadPoolExecutor( threadPool1.getCorePoolSize(), threadPool1.getMaximumPoolSize(), threadPool1.getKeepAliveTime(), TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(threadPool1.getCapacity()), namedThreadFactory, new ThreadPoolExecutor.DiscardPolicy() );; private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder() .setNameFormat("pool-%d").build(); public void execute(Runnable runnable){ threadPool.submit(runnable); } }
在yml文件的配置如下:
thread-pool: core-pool-size: 5 maximum-pool-size: 20 keep-alive-time: 1 capacity: 1024
本想应该毫无问题,但是,报错了:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myThreadUtils' defined in fileXXXXXXXXXX(省略)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
空指针异常?检查好几遍配置没错。因为公司开发环境没法上网,只好拖到下班google了一下,结合我比较深厚的基础(自恋一下),
问题轻松解决
这就是答案。上面说所有的Spring的@Autowired注解都在构造函数之后,而如果一个对象像下面代码一样声明(private XXX = new XXX() 直接在类中声明)的话,成员变量是在构造函数之前进行初始化的,甚至可以作为构造函数的参数。
即 成员变量初始化 -> Constructor -> @Autowired
所以,在这个时候如果成员变量初始化时调用了利用@Autowired注解初始化的对象时,必然会报空指针异常的啊。
真相大白了。如果解决呢?那就让上面我写的代码的成员变量threadPool在@Autowired之后执行就好了。
要想解决这个问题,首先要知道@Autowired的原理:
AutowiredAnnotationBeanPostProcessor 这个类
其实看到这个继承结构,我心中已经有解决办法了。具体详细为什么,等997的工作结束(无奈)我会在后续博客里将Spring的注解配置详细的捋一遍,到时候会讲到Bean的生命周期的。
继承的BeanFactoryAware是在属性赋值完成,执行构造方法后,postProcessBeforeInitialization才执行,而且,是在其他生命周期之前,而@Autowired注解就是依靠这个原理进行的自动注入。想要解决这个问题很简单,就是把要赋值的成员变量放到其他生命周期中就可以。
下面介绍其中两种办法
第一种JSR250的@PostConstruct
@PostConstruct public void init() { // 这里放要执行的赋值 }
第二种是Spring的InitializingBean(定义初始化逻辑)
继承接口实现方法即可,这种直接放上完整用法
/** * @author Yaqiang Chen */ @Component public 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<Runnable>(threadPool1.getCapacity()), namedThreadFactory, new ThreadPoolExecutor.DiscardPolicy() ); } }
设置完成后,问题解决!