[英]Spring @Bean configuration with constructor injection
While Executing below code, Spring Says "The dependencies of some of the beans in the application context form a cycle:"在执行下面的代码时,Spring 说“应用程序上下文中某些 bean 的依赖关系形成一个循环:”
@Configuration
public class Config {
private final Object object1;
public Config(@Qualifier("object1") Object object1) {
this.object1 = object1;
}
@Bean
@DependsOn("object1")
public String getName() {
return object1.toString();
}
@Bean(name = "object1")
public Object getObject() {
return new Object();
}
}
Can anyone explain me, How this form a cycle ?谁能解释一下,这是如何形成循环的?
The Config
class itself is a bean, which depends on your object1
bean, and which provides your object1
bean. Config
类本身是一个 bean,它依赖于您的object1
bean,并提供您的object1
bean。 (The error messages doesn't just say "some of the beans", you're getting a detailed list.) (错误消息不只是说“一些 bean”,你会得到一个详细的列表。)
tl;dr avoid injecting beans into configuration classes like this and make them @Bean
method parameters instead. tl;dr 避免将 bean 注入这样的配置类,而是将它们@Bean
方法参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.