AutoConfiguration to support creating multiple dynamic named beans
See original GitHub issueAutoConfiguration 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:
- Created 7 years ago
- Comments:11 (5 by maintainers)
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:
You’d have something like:
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.