英文:
How the dependency class constructor is invoked before the dependent class constructor in Spring?
问题
以下是翻译好的代码部分:
我使用Spring框架编写了以下代码。
**CODE**
package com.spring.core.trialDI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@ComponentScan("com.spring.core.trialDI")
class LegendConfig {
}
@Component
class A {
public A() {
System.out.println("我是A的构造函数!");
}
}
@Component
class B {
A a;
public B() {
System.out.println("我是B的构造函数!");
}
@Autowired
public void setA(@Qualifier("a") A a) {
System.out.println("我是B的setA!");
this.a = a;
}
}
@Component
class C {
A a;
public C() {
System.out.println("我是C的构造函数!");
}
@Autowired
public void setA(@Qualifier("a") A a) {
System.out.println("我是C的setA!");
this.a = a;
}
}
public class TrialTheory {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(LegendConfig.class);
B b = applicationContext.getBean(B.class);
C c = applicationContext.getBean(C.class);
}
}
OUTPUT
我是A的构造函数!
我是B的构造函数!
我是B的setA!
我是C的构造函数!
我是C的setA!
现在,请提出您的问题或继续讨论。
英文:
I have written the following code using Spring framework.
CODE
package com.spring.core.trialDI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@ComponentScan("com.spring.core.trialDI")
class LegendConfig {
}
@Component
class A {
public A() {
System.out.println("I am A constructor!");
}
}
@Component
class B {
A a;
public B() {
System.out.println("I am B constructor!");
}
@Autowired
public void setA(@Qualifier("a") A a) {
System.out.println("I am setA of B!");
this.a = a;
}
}
@Component
class C {
A a;
public C() {
System.out.println("I am C constructor!");
}
@Autowired
public void setA(@Qualifier("a") A a) {
System.out.println("I am setA of C!");
this.a = a;
}
}
public class TrialTheory {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(LegendConfig.class);
B b = applicationContext.getBean(B.class);
C c = applicationContext.getBean(C.class);
}
}
OUTPUT
I am A constructor!
I am B constructor!
I am setA of B!
I am C constructor!
I am setA of C!
Now as in main()
method, I have created a bean of B
type first, so the constructor of class B
must have called first even before the setter. (Constructor creates the object and its method can only be used after its creation).
But why A
s constructor is called before anyone? What's going on behind the scene?
答案1
得分: 1
Calling B b = applicationContext.getBean(B.class);
不意味着你创建一个 bean(如果它是一个单例 bean,就像你的示例一样)。它只会从上下文中给你一个已经创建的 bean。Spring 在启动时(在应用程序上下文创建阶段)创建单例 bean。
你可以在这里阅读更多关于Spring如何管理 bean 的信息:
https://docs.spring.io/spring-framework/reference/core/beans.html
英文:
Calling B b = applicationContext.getBean(B.class);
does not mean that you create a bean (if it's a singleton bean, as in your example). It only gives you an already created bean from context. Spring creates singleton beans during its startup (during the application context creation phase).
You can read more about how spring manages beans here:
https://docs.spring.io/spring-framework/reference/core/beans.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论