Arul,
You have a number of options:
1) Declare an @ExternalBean method to pull the desired beans in:
Code:
@Configuration
@AnnotationDrivenTx
@PropertiesValueSource(locations = "classpath:jdbc.properties")
@Import(DataSourceConfiguration.class)
@ComponentScan({"dao.impl", "service.impl"})
public abstract class JpaConfiguration {
//all the beans defined except service and DAO beans
abstract @ExternalBean PersonDao personDao();
public @Bean Foo beanThatNeedsPersonDao() {
return new Foo(personDao());
}
}
2) Extend ConfigurationSupport and use the getBean() method:
Code:
@Configuration
@AnnotationDrivenTx
@PropertiesValueSource(locations = "classpath:jdbc.properties")
@Import(DataSourceConfiguration.class)
@ComponentScan({"dao.impl", "service.impl"})
public class JpaConfiguration extends ConfigurationSupport {
//all the beans defined except service and DAO beans
public @Bean Foo beanThatNeedsPersonDao() {
return new Foo(getBean(PersonDao.class));
// or:
// return new Foo(getBean("personDao"));
}
}
3) Use @Autowired / @Resource
Remember - @Configuration classes end up being "just another spring bean" in the container, so are therefore candidates for autowiring just like all the rest! Note that using @AnnotationDrivenConfig is required here:
Code:
@Configuration
@AnnotationDrivenConfig
@AnnotationDrivenTx
@PropertiesValueSource(locations = "classpath:jdbc.properties")
@Import(DataSourceConfiguration.class)
@ComponentScan({"dao.impl", "service.impl"})
public class JpaConfiguration {
//all the beans defined except service and DAO beans
private @Autowired PersonDao personDao;
public @Bean Foo beanThatNeedsPersonDao() {
return new Foo(personDao);
}
}
I'll be interested to hear which approach you find most compelling