[英]Spring autowiring order and @PostConstruct
I have a question about auto-wiring order and @PostConstruct
logic in Spring. 我对Spring中的自动布线顺序和
@PostConstruct
逻辑有疑问。 For example following demo code I have a main Spring Boot class: 例如,下面的演示代码我有一个主要的Spring Boot类:
@SpringBootApplication
public class Demo1Application {
@Autowired
BeanB beanb;
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
and 2 @Service
Definitions: 和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();
}
}
and I have the following output: 我有以下输出:
bean a is called
豆a被称为
print me is called in Bean B
打印我在Bean B中调用
beanb is called
beanb被称为
My question is how autowiring takes place step by step like a scenario above? 我的问题是如何像上面的场景一样一步一步地进行自动装配?
And how printMe()
method of beanb
is called without calling its @PostConstruct
first? 而如何
printMe()
的方法beanb
被称为不调用其@PostConstruct
第一?
Below should be possible sequence 下面应该是可能的顺序
beanb
starts to get autowired beanb
开始自动装配 Beanb
, beana starts to get autowired Beanb
类初始化期间,beana开始自动装配 @PostConstruct
ie init()
of beana gets called @PostConstruct
即beana的init()
被调用 init()
, System.out.println("bean a is called");
init()
, System.out.println("bean a is called");
gets called b.printMe();
b.printMe();
gets called causing System.out.println("print me is called in Bean B");
System.out.println("print me is called in Bean B");
to execute beana
completed the @PostConstruct
ie init()
of beanb
gets called beana
完成了@PostConstruct
即init()
的beanb
被调用 System.out.println("beanb is called");
System.out.println("beanb is called");
gets called Ideally the same can be better observed by a debugger in eclipse. 理想情况下,eclipse中的调试器可以更好地观察到相同的情况。
The Spring reference manual explains how circular dependencies are resolved. Spring参考手册解释了如何解决循环依赖关系。 The beans are instantiated first, then injected into each other.
首先实例化bean,然后相互注入。
Your Answer is Correct as you shown in Your question. 您的答案是正确的,如您在问题中所示。
Now Getting the concept of Notation @Autowired
. 现在获得符号
@Autowired
的概念。 All @Autowired
Objects are initialized and loaded in memory just after class Loading is done. 在完成类加载后,所有
@Autowired
对象都被初始化并加载到内存中。
Now here is your SpringBootApplication
现在这是你的
SpringBootApplication
@SpringBootApplication
public class Demo1Application {
@Autowired
BeanB beanb; // You are trying to autowire a Bean class Named BeanB.
Here at above Console Application that you have write try to autowire and inject a object of type BeanB
. 在上面的控制台应用程序中,您尝试自动装配并注入
BeanB
类型的对象。
Now here is your definition of BeanB
现在,这是您对
BeanB
的定义
@Service
public class BeanB {
@Autowired
private BeanA beana ;
In BeanB
class you are trying to inject the Object of Class BeanA
which is also defined in your console Project. 在
BeanB
类中,您尝试注入类BeanA
的Object,它也在您的控制台Project中定义。
So, In Your Demo1Application
to inject a Object of Class BeanB
there must need to inject a Object of class BeanA
. 因此,在您的
Demo1Application
中注入类BeanB
的Object必须注入类BeanA
的Object。 Now BeanA
Class Object is Created First. 现在首先创建
BeanA
类对象。
Now if you see the definition of Your Class BeanA
现在,如果你看到你的Class
BeanA
的定义
@Service
public class BeanA {
@Autowired
private BeanB b;
@PostConstruct // after Creating bean init() will be execute.
public void init(){
System.out.println("bean a is called");
b.printMe();
}
}
So, After injecting the Object BeanA
method bind with @PostContruct
annotation is going to execute. 因此,在注入Object
BeanA
方法之后,绑定@PostContruct
注释将会执行。
So, execution flow will be.. 所以,执行流程将是......
System.out.println("bean a is called");
System.out.println("print me is called in Bean B");
System.out.println("beanb is called");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.