Spring中如何在依赖类构造函数之前调用依赖类构造函数?

huangapple go评论68阅读模式
英文:

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 As 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

huangapple
  • 本文由 发表于 2023年5月24日 19:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定