我使用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("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!
现在和在main()
方法中一样,我首先创建了一个B
类型的Bean,所以class B
的构造函数必须在setter之前先调用.(构造函数创建对象,其方法只能在创建后使用).
但是,为什么在任何人之前调用A
构造函数呢?幕后发生了什么?