Dynamicly create multiple Beans from List of Values imported from ConigurationFile

Viewed 39

I want to dynamicly create multiple Beans from values of a configuration file.

Therefore I have an AutoConfiguration where I can set up beans:

@Configuration
public class ExampleAutoConfiguration {

    private final PropertiesFromConfigurationFile properties;

    public AutoConfiguration(PropertiesFromConfigurationFile properties) {
        this.properties = properties;
    }
}

I can create a single Bean with:

@Bean
public MyInterface myBean() {
        return new MyBean(properties.getSauce());
}

But instead I want to create multiple Beans by mapping over a list of Properties

@Data
public class PropertiesFromConfigurationFile {
    String sauce; // OLD
    List<String> listOfSauces; // NEW -> ["foo", "bar"]
}

Then I have a set of Controller classes that should autowire those Beans.

@Controller
public class ExampleController {

    @Qualifier("fooMyBean")
    private final MyInterface myBean;

    public ExampleController(MyBean myBean) {
        this.myBean = myBean;
    }
}

@Controller
public class AnotherExampleController {

    @Qualifier("barMyBean")
    private final MyInterface myBean;

    public AnotherExampleController(MyBean myBean) {
        this.myBean = myBean;
    }
}

How can I achieve this?

0 Answers
Related