Using @EachBean
Using @EachBean
@Factory (1)
class DataSourceFactory {
@EachBean(DataSourceConfiguration.class) (2)
DataSource dataSource(DataSourceConfiguration configuration) { (3)
URI url = configuration.getUrl()
}
Note that @EachBean
requires that the parent bean has a @Named
qualifier, since the qualifier is inherited by each bean created by @EachBean
.
In other words, to retrieve the DataSource
created by test.datasource.one
you can do:
Collection<DataSource> beansOfType = applicationContext.getBeansOfType(DataSource.class);
assertEquals(2, beansOfType.size()); (1)
DataSource.class,
);
Using a Qualifier
Using a Qualifier
val beansOfType = applicationContext.getBeansOfType(DataSource::class.java)
assertEquals(2, beansOfType.size.toLong()) (1)
val firstConfig = applicationContext.getBean(
DataSource::class.java,
)
1 | We demonstrate here that there are indeed two data sources. How can we get one in particular? |
2 | By using , we can select which of the two beans we’d like to reference. |