我看不懂下面这句话的粗体部分,也许是整句话:
try 将@Bean方法的结果注入同一配置类也是一种有效的自引用场景.否则,此类Bean仅在备用阶段被考虑,其他配置类上的匹配Bean被选为主要候选(如果可用).
To ensure that I understand this sentence correctly, let me first explain my understanding.个
自引用是指Bean A在相同的配置类中调用Bean B,或者使用其他Bean作为参数,对吗?
像这样.
@Configuration
public class JavaConfig {
@Bean
Course course() {
Course course = new Course();
return course;
}
@Bean
Teacher teacher(Course course) { // self-reference
Teacher teacher = new Teacher();
Course course1 = course(); // self-reference
System.out.println(course);
teacher.setCourse(course);
return teacher;
}
@Bean
String[] courseList() {
String[] cs = {"OS", "Network"};
return cs;
}
}
public class Course {
@Resource(name = "courseList")
private String[] courseList;
}
public class Teacher {
private Course course;
public void setCourse(Course course) {
this.course = course;
}
@Override
public String toString() {
return "Teacher{" +
"course=" + course +
'}';
}
}
如果我上面的概念描述是正确的,那么我可以继续文档中描述的解决方案.
-
将受影响的@Bean方法声明为静态方法.
我认为这应该是正确的,
CGLIB
直接调用static Course course
方法,而不是调用proxy object
.
@Configuration
public class JavaConfig {
@Bean
static Course course() { // add static qualifier
Course course = new Course();
return course;
}
// ...
}
-
或者懒惰地在方法签名中实际需要的地方解析这样的引用(as opposed to an autowired field in the configuration class)
关于这一点,我不确定如何实施.我要加
@Lazy
吗?这似乎不切实际,我不确定我是否遗漏了任何概念.