Spring从应用程序动态创建bean。属性[英] Spring - Dynamically Creating Beans from application.properties

问题描述

我需要使用属性文件创建多个同名 bean.例如:

rabbit.example.language[0]=java
rabbit.example.framework[0]=spring
rabbit.example.language[1]=python
rabbit.example.framework[1]=django

我想为每个示例索引创建一个 Bean(这最终将用于 Rabbit 队列,但我正在简化事情).这是到目前为止我正在使用的一些代码.

Rabbits.java

@Configuration
@ConfigurationProperties(prefix="example")
public class Rabbits {
    private String language;
    private String framework;

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language= language;
    }

    public String getFramework() {
        return framework;
    }

    public void setFramework(String framework) {
        this.framework= framework;
    }
}

Properties.java

@Configuration
@ConfigurationProperties(prefix = "rabbit")
public class Properties {

    @NestedConfigurationProperty
    private List<Rabbits> rabbits = new ArrayList<Rabbits>();

    public List<Rabbits> getRabbits() {
        return rabbits;
    }

    public void setRabbits(List<Rabbits> rabbits) {
        this.rabbits = rabbits;
    }

    @Bean
    RabbitFlowProcessor rabbitFlowProcessor(List<Rabbits> rabbit){
        return new RabbitFlowProcessor(rabbit);
    }

}

RabbitFlowProcessor.java:

@Component
public class RabbitFlowProcessor implements BeanFactoryPostProcessor {

    private List<Rabbits> rabbits;

    public RabbitFlowProcessor(List<Rabbits> rabbits) {
        this.rabbits = rabbits;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory  beanFactory) throws BeansException {
      rabbits.stream()
              .forEach(rabbit -> {
                  System.out.println(rabbit.getLanguage());
              });
    }
}

我希望输出是 Java Python,但"兔子"返回为空.谁能告诉我哪里出错了?

推荐答案

请更新您的代码并添加以下评论:

rabbit.example[0].language=java
rabbit.example[0].framework=spring
rabbit.example[1].language=python
rabbit.example[1].framework=django

由于示例将是一个列表,因此您需要添加索引.

<小时>
@Configuration
@ConfigurationProperties(prefix = "rabbit")
public class RabbitsProperties {

    private List<Rabbits> example = new ArrayList<Rabbits>();

    public List<Rabbits> getExample() {
        return example;
    }

    public void setExample(List<Rabbits> example) {
        this.example = example;
    }
}

关于 NestedConfigurationProperty 注释,它表示 ConfigurationProperties 对象中的字段应该被视为嵌套类型.此注解与实际绑定过程无关,但它被 spring-boot-configuration-processor 用作字段未绑定为单个值的提示.

<小时>
public class Rabbits {
    private String language;
    private String framework;

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language= language;
    }

    public String getFramework() {
        return framework;
    }

    public void setFramework(String framework) {
        this.framework= framework;
    }
}

通常,您可以将其添加为 RabbitsProperties

中的嵌套内部类

然后在你的RabbitFlowProcessor.java中,你可以像往常一样注入RabbitsProperties.

@Component
public class RabbitFlowProcessor implements BeanFactoryPostProcessor {

    @Autowired
    private RabbitsProperties rabbitsProperties; 
    ......
}

本文地址:https://www.itbaoku.cn/post/978537.html