[英]Compile-Time and Load-Time Weaving and Autowired on XMLAdapter not working
[英]compile time weaving and autowired on constructor
@Configurable
public TestClass(){
@Autowired SomeOtherClass otherClass;
public TestClass(Var1 var){
System.out.println(otherClass);
}
}
我使用编译时编织并调用new testClass(var)
。 我像上面的“ otherClass”一样在构造函数上使用依赖注入。 它打印出“ null”。 根据我的测试,依赖项注入“ otherClass”无法在构造函数中初始化。 我应该怎么做才能允许对“构造函数”进行依赖注入?
查看Spring文档本节中的Note。
它显示了@Configurable
属性,用于强制在构造函数中的代码之前进行注入。
@Configurable(preConstruction=true)
在上面的代码中添加该属性将停止System.out.println(otherClass);
从为null
。
我猜(但不知道100%)在创建对象之后进行自动装配。 ( 我的意思是对于普通的Spring Bean,普通的类我都知道,但是对于@Configurable却不是100% )
这意味着您不能期望在创建对象时就已经准备好自动接线字段。
如果添加在创建后调用的第二个方法printStats,则可以检查此内容。
public class TestClass {
...
public void printStats(){
System.out.println(otherClass);
}
}
...
new TestClass(var).printStats();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.