我对Spring中自动装配顺序和@PostConstruct
逻辑有疑问。例如,以下是我的一个主要Spring Boot类的演示代码:
@SpringBootApplication
public class Demo1Application {
@Autowired
BeanB beanb;
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
还有2个 @Service
定义:
@Service
public class BeanB {
@Autowired
private BeanA beana ;
@PostConstruct
public void init(){
System.out.println("beanb is called");
}
public void printMe(){
System.out.println("print me is called in Bean B");
}
}
@Service
public class BeanA {
@Autowired
private BeanB b;
@PostConstruct
public void init(){
System.out.println("bean a is called");
b.printMe();
}
}
我得到了以下输出:
调用了bean a
在Bean B中调用了print me方法
调用了beanb
我的问题是自动装配是如何像上面的场景一步步发生的?
以及为什么可以在没有先调用其@PostConstruct
方法的情况下调用beanb
的printMe()
方法?