Hello guys,
I like the new Java Beans Config and used it in a simple project already. Now I'm trying to use that configuration style in a bigger project. I tried to use a custom FactoryBean and stumbled across two alternatives for expressing dependencies between beans. Imaging the following sample:
This is the style used in the spring documentation. Nevertheless I've also seen a blog post and forum posts using the following style:Code:@Configuration @Import(DaoConfiguration.class) public class Configuration { @Autowired private DaoInterface dao @Bean public Service myService() { return new MyService(this.dao); } }
I tried it and it works like a charm! But I didn't find any comment about that style in the spring reference documentation. I'm wondering whether this approach has any draw backs or possible pitfalls? What is the recommended approach for expressing dependencies using java beans config?Code:@Configuration @Import(DaoConfiguration.class) public class Configuration { @Bean public Service myService(final DaoInterface dao) { return new MyService(dao); } }
One benefit of the second approach is the support for FactoryBeans! Imagine the following DaoConfiguration:
The DaoConfiguration above does work with the second Configuarion implementation, but not with the first one. So why is the argument-style of expressing dependencies not explained in Spring's reference documentation? What are the best practices using java beans config? Which style of configuration should be used?Code:@Configuration public class DaoConfiguration { @Bean public MyFactoryBean dao() { MyFactoryBean factory = new MyFactoryBean(); // implements Springs FactoryBean factory.forDao(DaoInterface.class); return factory; } }
Thanks for any comments,
Kyle


Reply With Quote
