Spring: Make sure a particular bean gets initialized first
You can @Autowired an @Configuration in the main @Configuration
@Configuration
@Import(BusinessConfig.class, EarlyBeans.class)
public class MainConfiguration {
// The bean defined in EarlyBean will be loaded before
// most beans references by MainConfiguration,
// including those coming from BusinessConfig
@Autowired
EarlyBeans earlyBeans;
}
@Configuration
public class EarlyBeans {
@Bean
public Void earlyBean(ApplicationContext appContext) {
// .getBeansOfType allows to call for beans which might not exist
appContext.getBeansOfType(TechnicalBean.class);
return null;
}
}
Your options are:
- Use
@DependsOn
annotation(available after spring 3.0.x) ordepends-on
xml-attribute and make all classes that use the configured loggers depend on the logger factory - Make the factory an actual factory for loggers, and inject the loggers into the beans instead of calling the factory directly – this is essentially the same as option 1, except the dependency is implied. This is the option I'd recommend.
- Move the initialisation code to a part of your code where call order is specified – the
main()
method, or aServletContextListener
registered before the one that initializes Spring.
There is no way to explicitly define initialisation order in Spring and likely never will be – there's no way to define useful semantics for it considering you can load many application context configuration files which might have conflicting orderings. I've yet to see a case where the desired ordering couldn't be achieved by refactoring your code to better conform to the dependency injection pattern.