证明 @Autowired 不是自动装配
记得上一篇博客 Spring源码系列 - 依赖注入(Dependency Injection),我有说 @Autowired 不属于自动装配的范畴,只属于手动装配,有同学表示不可理解,毁三观,再此我会用代码来证明,Talk is cheap, show me the code
上代码前,我们先看一段 Spring 官方给除的 bean 定义:
Autowiring mode:表示 自动装配的模型,用一个整形变量来表示
0:表示 no(不装配)
1:表示 byName(按名称装配)
2:表示 byType(按类型装配)
3:表示 constructor(根据构造函数装配)
那现在我们就知道了,每一个 Spring Bean 都有一个 Autowiring 字段,用来表示该 Bean 的装配模式
现在我们先来看代码,下面这段代码先注释掉了 @Autowired 注解,我们先看看这个 Bean 的装配模式是什么?
@Component
public class IndexService {
// @Autowired
UserService userService;
public IndexService() {
System.out.println("Constructor from IndexService");
}
@PostConstruct
public void init() {
System.out.print