question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AutoConfiguration to support creating multiple dynamic named beans

See original GitHub issue

AutoConfiguration is awesome for setting up beans you typically only need one instance, such as a DataSource etc.

I am looking for a way of allowing the end user to

  • allow to specify the name of the bean, such as from application.yml / .properties file
  • create multiple bean instances of same type but with different names

For example in your application.properties file you can setup two beans of Dude type being named foo and bar as follows:

dude.foo.timeout=10000
dude.foo.verbose=false

dude.bar.timeout=20000
dude.bar.verbose=true

In this example there is an AutoConfiguration that can create beans of Dude type.

    @Bean
    public Dude configureDude(DudeConfiguration configuration) throws Exception {
      ...
    }

What I would like to see possible is somehow to tell Spring Boot that the DudeConfiguration supports dynamic name, where you can specify this in the prefix such as:

For example using an .* to denote a dynamic name

@ConfigurationProperties(prefix = "dude.*")
public class DudeConfiguration {

or maybe better to use a REST like binding using {id} style

@ConfigurationProperties(prefix = "dude.{name}")
public class DudeConfiguration {

The DudeAutoConfiguration class would then need to support {name} for example as a parameter in the method signature of the @Bean that can create the beans, such as String name below:

    @Bean
    public Dude configureDude(DudeConfiguration configuration, String name) throws Exception {
      ...
    }

There is maybe a better way. But its important that the value of the name is available in the configure method so we can use that in the code to create the configured dude instance to be returned.

Also maybe @Bean needs to highlight that the method creates dynamic named beans, so you should maybe specify this as Bean(name = "{name}") or something.

For more background details then I work on Apache Camel where we are adding much love for Spring Boot to allow to configure Camel using Spring’s AutoConfiguration. The missing piece we need is to be able to let end users configure Camel endpoints, where you can have multiple endpoints of the same type (eg JMS, File, RabbitMQ) but configured differently. And the name of these endpoints is a name that the end users specify.

We discussed this in the Camel ticket at: https://issues.apache.org/jira/browse/CAMEL-10031

We have looked at an out of the box solution with current Spring Boot and could not find one, either does the Map/List type in AutoConfiguration not work well. Tooling in IDEA,Eclipse etc. does neither work well with them as they do not provide auto completion / default values / documentation. We could not find any existing ticket reported about this enhancement. A related ticket we could find was: https://github.com/spring-projects/spring-boot/issues/2687

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Jul 28, 2016

I meant value as in the value of a property. It’s really about how you structure your properties or YAML. To be tooling friendly, you want the keys to be finite and well-known. In other words, by not having part of a key by a name that a user can choose. Concretely, rather than:

dude.foo.timeout=10000
dude.foo.verbose=false

dude.bar.timeout=20000
dude.bar.verbose=true

You’d have something like:

dude[0].name=foo
dude[0].timeout=10000
dude[0].verbose=false

dude[1].name=bar
dude[1].timeout=20000
dude[1].verbose=true
0reactions
snicollcommented, Aug 2, 2016

We’re not dealing with IntelliJ IDEA (nor any other IDEs) in this tracker. Please report that at the appropriate place. @nicolaferraro this is perfectly normal. What would you use expect us to do, we’d have an infinite number of keys. There is already several requests to support browsing POJOs from list and maps so search in their tracker.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create beans dynamically in spring custom starter ...
How to create beans dynamically in spring custom starter based on property ; @ConditionalOnClass(MyBeanFactory.class) public ; class ...
Read more >
Instantiating Multiple Beans of the Same Class with Spring ...
In this tutorial, we'll learn how to use annotations in the Spring framework to create multiple beans of the same class.
Read more >
Spring Boot Auto-configuration - Javatpoint
Spring Boot auto-configuration automatically configures the Spring application based on the jar dependencies that we have added.
Read more >
Choosing from multiple beans in the context - Manning
Figure 2. When the Spring context contains multiple beans of the same type, Spring selects the bean whose name matches the name of...
Read more >
Spring Boot - Auto-configuration - GeeksforGeeks
It automatically registers the beans with @Component, @Configuration, @Bean, and meta-annotations for building custom stereotype annotations, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found