Announcement Announcement Module
Collapse
No announcement yet.
@Configuration and @Autowired dependencies Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configuration and @Autowired dependencies

    Hi.

    We are slowly trying to move to javaconfig from XML based config.

    We have a number of @Configuration classes, which are loaded up together with an AnnotationConfigApplicationContext, that scans packages looking for those configurations.

    So far, so good.

    One of those configurations might be like the one in the example :
    @Configuration
    public class AppConfig {
    private @Autowired DataSource dataSource;

    public @Bean AccountRepository accountRepository() {
    return new JdbcAccountRepository(dataSource);
    }

    public @Bean TransferService transferService() {
    return new TransferService(accountRepository());
    }
    }
    However! When the @Beans are created, the DataSource has not yet been Autowired! So nothing works...

    I can see why -- it's impossible for Spring to know about that dependency. My question is how to alleviate this problem.

    The only indications that I can see is that an explicit dependency could be added (with @Import) to the configuration defining DataSource, or the configuration itself could be injected into this configuration, and 'dataSource' replaced with 'datasourceConfiguration.dataSource()'.

    Both of those mechanisms feel pretty horrible. I don't want configurations knowing about each other - that totally breaks the modularity of the system (and one of the main reasons for wanting DI in the first place).

    Is there no other mechanism (perhaps taking advantage of the CGLIB proxy the @Configuration is in) ? Or by throwing an exception when the dependency is not yet initialized ?
Working...
X