Spring: Make sure a particular bean gets initialized first

JavaSpring

Java Problem Overview


I have a library doing runtime setup and configuration of log4j (no log4j.properties or log4j.xml). I have defined a bean with class called MyLoggerFactory and I want this to be the first bean to be initialised using spring. I have seen that an issue has already been filed with spring to have support for order of initialisation but I was wondering whether there was a way to mark a bean as the first bean to be initialised by spring container?

Java Solutions


Solution 1 - Java

Your options are:

  1. Use @DependsOn annotation(available after spring 3.0.x) or depends-on xml-attribute and make all classes that use the configured loggers depend on the logger factory
  2. 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.
  3. Move the initialisation code to a part of your code where call order is specified – the main() method, or a ServletContextListener 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.

Solution 2 - Java

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;
	}

}

Solution 3 - Java

This is a feature requested but not resolved. You can use depends-on but is too verbose. Follow tis link for more information: https://jira.springsource.org/browse/SPR-3948

Solution 4 - Java

You can split your application context as multiple and use import in main application context. You can put the main environment settings first in the order of import and then continue adding other files.

It could be like below.

<!-- Import environment properties settings. -->
<import resource="Spring-Env.xml"/>
<!-- Import All the other Application contexts. -->
<import resource="Spring-MainApplicationContext.xml"/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrasannaView Question on Stackoverflow
Solution 1 - JavamillimooseView Answer on Stackoverflow
Solution 2 - JavablacelleView Answer on Stackoverflow
Solution 3 - JavaMiguel PrzView Answer on Stackoverflow
Solution 4 - JavaHari MView Answer on Stackoverflow