Spring - 从application.properties动态创建Bean

时间:2016-09-08 00:59:47

标签: java spring

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

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

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

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,但是&#34;兔子&#34;回来是空的。谁能告诉我哪里出错?

1 个答案:

答案 0 :(得分:2)

请使用以下评论更新您的代码:

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

由于示例将是List,因此您需要使用它添加索引。

@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 annoation,表示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; 
    ......
}