这篇文章主要介绍了@Autowired注入空指针问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇@Autowired注入空指针问题如何解决文章都会有所收获,下面我们一起来看看吧。
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
这就是答案。上面说所有的Spring的@Autowired注解都在构造函数之后,而如果一个对象像下面代码一样声明(private XXX = new XXX() 直接在类中声明)的话,成员变量是在构造函数之前进行初始化的,甚至可以作为构造函数的参数。
即 成员变量初始化 -> Constructor -> @Autowired
所以,在这个时候如果成员变量初始化时调用了利用@Autowired注解初始化的对象时,必然会报空指针异常的啊。
真相大白了。如果解决呢?那就让上面我写的代码的成员变量threadPool在@Autowired之后执行就好了。
要想解决这个问题,首先要知道@Autowired的原理:
AutowiredAnnotationBeanPostProcessor 这个类
其实看到这个继承结构,我心中已经有解决办法了。具体详细为什么,等997的工作结束(无奈)我会在后续博客里将Spring的注解配置详细的捋一遍,到时候会讲到Bean的生命周期的。
继承的BeanFactoryAware是在属性赋值完成,执行构造方法后,postProcessBeforeInitialization才执行,而且,是在其他生命周期之前,而@Autowired注解就是依靠这个原理进行的自动注入。想要解决这个问题很简单,就是把要赋值的成员变量放到其他生命周期中就可以。
@PostConstruct public void init() { // 这里放要执行的赋值 }
继承接口实现方法即可,这种直接放上完整用法
/** * @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() ); } }
关于“@Autowired注入空指针问题如何解决”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“@Autowired注入空指针问题如何解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。