Using @EachBean

    Using @EachBean

    1. @Factory (1)
    2. class DataSourceFactory {
    3. @EachBean(DataSourceConfiguration.class) (2)
    4. DataSource dataSource(DataSourceConfiguration configuration) { (3)
    5. URI url = configuration.getUrl()
    6. }

    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:

    1. Collection<DataSource> beansOfType = applicationContext.getBeansOfType(DataSource.class);
    2. assertEquals(2, beansOfType.size()); (1)
    3. DataSource.class,
    4. );

    Using a Qualifier

    Using a Qualifier

    1. val beansOfType = applicationContext.getBeansOfType(DataSource::class.java)
    2. assertEquals(2, beansOfType.size.toLong()) (1)
    3. val firstConfig = applicationContext.getBean(
    4. DataSource::class.java,
    5. )
    1We demonstrate here that there are indeed two data sources. How can we get one in particular?
    2By using , we can select which of the two beans we’d like to reference.