这两天用idea写spring注入的时候每一次
@Autowired
Worker worker;
都会报黄,用过这个ide的都知道,说明你代码需要重构了。
然后提示的信息是
Spring Team recommends: “Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies”大致意思是 ,每一次的依赖注入都用构造方法吧,并且每一次对强制依赖关系使用断言,大白话就是我们这样可以在构造方法里面做点校验了,这样在spring容器启动的时候就会发现错误。就类似于编译器那些在编译器就可以发现的错误,这样防止在使用的时候出现运行时异常。导致程序崩掉。强制依赖关系可以理解为,B的方法中调用A的方法
群里面还发了个例子
// Fieldinjection/NullPointerException example:
class MyComponent {@Inject MyCollaborator collaborator;
public void myBusinessMethod() {
collaborator.doSomething();
// -> NullPointerException
}
}
Constructor injection should be used:
class MyComponent {private final MyCollaborator collaborator;
@Inject
public MyComponent(MyCollaborator collaborator) {
Assert.notNull(collaborator, "MyCollaborator must not be null!");
this.collaborator = collaborator;
}public void myBusinessMethod() {
collaborator.doSomething();
// -> safe
}
}
本来实现一个null的bean很简单
显示用BeanFactoryPostProcessor实现,发现如果添加一个null会直接报错
因为在register里面会进行判断
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
后来无奈搜索引擎一发
发现了可以 factorybean一发(原理后面补,,最近在研究源码,还没看这一块)
@Component
public class Worker implements FactoryBean {public void doWork(){
System.out.println("do Work...");
}@Override
public Void getObject() throws Exception {
return null;
}@Override
public Class> getObjectType() {
return Worker.class;
}@Override
public boolean isSingleton() {
return true;
}
}
那么我的依赖注入先这么写
@Component
public class Factory {
@Autowired Worker worker;
public void createProduct(){
System.out.println("生产中....");
worker.doWork();
System.out.println("生产完成...");
}
}
运行,可以运行,报NPL
打印结果
生产中....
Exception in thread "main" java.lang.NullPointerException
@Component
public class Factory {
final Worker worker;
@Autowired
public Factory(Worker worker) {
Assert.notNull(worker, "worker must not be null!");
this.worker = worker;
}public void createProduct(){
System.out.println("生产中....");
worker.doWork();
System.out.println("生产完成...");
}
}
结果 无法运行,容器启动失败
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'factory' defined in file [F:\code\github\2017-up\spring-code\target\classes\com\wuhulala\studySpring\testnull\Factory.class]: Bean instantiation via constructor failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.wuhulala.studySpring.testnull.Factory]: Constructor threw exception;
nested exception is java.lang.IllegalArgumentException: worker must not be null!
【spring|spring bean-- autowired的正确用法】善用断言,在程序中启动的时候就发现错误。
推荐阅读
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- jar|springboot项目打成jar包和war包,并部署(快速打包部署)
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- java人生|35K 入职华为Java开发那天,我哭了(这 5 个月做的一切都值了)
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- 微服务|微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)
- java|ApplicationListener和SpringApplicationRunListener的联系
- Spring|SpringSecurity--自定义登录页面、注销登录配置
- 性能|性能工具之 Jmeter 通过 SpringBoot 工程启动
- 代码狂魔|Spring源码分析之IOC容器初始化流程