简体   繁体   English

Spring @Bean 配置与构造函数注入

[英]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.

相关问题 在bean的春天构造函数注入。 它又变成了豆子吗? - Spring constructor injection in bean. Is it a bean anymore? 通过@Configuration、@Bean 和 @Component 进行 Spring 依赖注入 - Spring Dependency Injection via @Configuration, @Bean and @Component 具有xml配置的构造函数注入的Spring装饰器 - Spring decorator with constructor injection with xml configuration Spring Boot:使用xml进行构造函数注入给出了“已经有n bean方法” - Spring boot : constructor injection with xml gives “there is already n bean method” spring - 构造函数注入和覆盖嵌套bean的父定义 - spring - constructor injection and overriding parent definition of nested bean 当bean有varargs构造函数时,如何使用XML配置Spring bean以进行构造函数注入 - How to XML configure Spring bean for constructor injection when bean has varargs constructor Spring:构造函数注入具有基于注释的配置的原始值(属性) - Spring: constructor injection of primitive values (properties) with annotation based configuration 春豆田注射 - Spring bean fields injection Spring Bean Injection中的ClassNotFoundException - ClassNotFoundException at Spring Bean Injection Spring中的Bean依赖注入 - Bean dependency injection in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM