Spring - 从application.properties动态创建Bean [英] Spring - Dynamically Creating Beans from application.properties

查看:781
本文介绍了Spring - 从application.properties动态创建Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a need to create multiple beans of the same name using properties file. For example:

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

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

I'd like to create a Bean for each example index (this will ultimately be used for Rabbit Queues, but I'm simplifying things). Here's some code I'm working with so far.

Rabbits.java

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

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:

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,但是兔子又回来了。谁能告诉我哪里出错?

I'm expecting the output to be Java Python, but the "rabbits" are coming back as null. Can anyone tell where I'm going wrong?

推荐答案

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

Please update your codes with following comments:

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

作为示例将是一个List,因此您需要使用它添加索引。

As the example will be a List, so you need add the index with it.

@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 使用它作为字段未绑定的提示作为单个值。

About the NestedConfigurationProperty annoation, that indicates that a field in a ConfigurationProperties object should be treated as if it were a nested type. This annotation has no bearing on the actual binding processes, but it is used by the spring-boot-configuration-processor as a hint that a field is not bound as a single value.

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

typically, you can add this as a nested inner class in RabbitsProperties

然后在你的 RabbitFlowProcessor.java 中,您可以照常注入 RabbitsProperties

Then in your RabbitFlowProcessor.java, you can inject the RabbitsProperties as normal.

@Component
public class RabbitFlowProcessor implements BeanFactoryPostProcessor {

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

这篇关于Spring - 从application.properties动态创建Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆