I have a question about the semantics of @Bean definitions within a @Configuration.
Let's say I have:
I define three beans: car, engine, and carService. CommonCarConfig has some common beans (car, engine) to many apps. One app (the app that uses MyCarAppConfig) needs to redefine one of the common beans. This is normal in Spring, right? So I "redefine" the engine bean to use fast engines instead of slow engines.Code:@Configuration public class CommonCarConfig { @Bean public car() { return new car(engine()); } @Bean public engine() { return slowEngine(); } } @Configuration @Import(CommonCarConfig.class) public class MyCarAppConfig { @Resource CommonCarConfig commonCfg; // redefine bean to replace all engines with fast engines @Bean public engine() { return fastEngine(); } @Bean public carService() { return new carService(commonCfg.car()); } }
Should my car service have a slow engine or a fast engine?
In my test, I get a slow engine...which seems semantically wrong. How would I accomplish this in the Java based configuration?
Thanks!
Steve


Reply With Quote