关于@Autowired注入空指针问题的解决(利用Bean的生命周期)

今天做项目的时候遇到一个问题,需要将线程池的参数抽取到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.NullPointerException
Caused 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()
        );
    }
}

设置完成后,问题解决!

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hermione Granger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值