我的Spring应用程序中有一些奇怪的行为,这是Java Spring Boot应用程序的结构:
在包装中com.somethingsomething.packageA,我有2个文件
首先是ParentA.java
package com.somethingsomething.packageA;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ParentA {
@Autowired
private ChildA childA;
public ChildA getChildA() {
return childA;
}
@PostConstruct
public void ParentAPostConstruct() {
System.out.println("ParentA PostConstruct were called");
}
}
第二个是ChildA.java
package com.somethingsomething.packageA;
import org.springframework.stereotype.Component;
@Component
public class ChildA {
public ChildA() {
System.out.println("ChildA were called");
}
}
然后在package下com.somethingsomething.packageB,我也有两个类似的文件。
首先是ParentB.java
package com.somethingsomething.packageB;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ParentB {
@Autowired
private ChildB childB;
public ChildB getChildB() {
return childB;
}
@PostConstruct
public void ParentBPostConstruct() {
System.out.println("ParentB PostConstruct were called");
}
}
第二个是ChildB.java
package com.somethingsomething.packageB;
import org.springframework.stereotype.Component;
@Component
public class ChildB {
public ChildB() {
System.out.println("ChildB were called");
}
}
但是在这种情况下ChildA were called
并ParentA PostConstruct were called
没有被假设要记录。
那么,为什么会发生这种特殊的行为呢?这是Spring的默认行为吗?
互换的青春
江户川乱折腾
相关分类