Controlling Bean Initialization Order in Spring: Using @Autowired, @Resource, @Inject and @DependsOn
This article explains how Spring developers can explicitly control bean instantiation order by using dependency‑injection annotations such as @Autowired, @Resource, @Inject, and the @DependsOn annotation, detailing the underlying processors and showing code examples for each mechanism.
In Spring, the order in which beans are instantiated can be explicitly controlled using dependency‑injection annotations such as @Autowired , @Resource and @Inject , which cause the container to create and inject the required beans before the current bean is fully initialized.
The implementation behind @Autowired and @Inject is provided by AutowiredAnnotationBeanPostProcessor , which also supports @Value injection. The source code of its constructor demonstrates how the processor registers the supported annotation types:
/**
* Create a new {@code AutowiredAnnotationBeanPostProcessor} for Spring's
* standard {@link Autowired @Autowired} and {@link Value @Value} annotations.
*
Also supports JSR-330's {@link javax.inject.Inject @Inject} annotation,
* if available.
*/
@SuppressWarnings("unchecked")
public AutowiredAnnotationBeanPostProcessor() {
this.autowiredAnnotationTypes.add(Autowired.class);
this.autowiredAnnotationTypes.add(Value.class);
try {
this.autowiredAnnotationTypes.add((Class
)
ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
logger.trace("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}The @Resource annotation is handled by CommonAnnotationBeanPostProcessor . Its static block registers the annotation types, including optional JSR‑250 and JSR‑330 annotations such as javax.xml.ws.WebServiceRef and javax.ejb.EJB , if they are present on the classpath:
static {
resourceAnnotationTypes.add(Resource.class);
webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef");
if (webServiceRefClass != null) {
resourceAnnotationTypes.add(webServiceRefClass);
}
ejbClass = loadAnnotationType("javax.ejb.EJB");
if (ejbClass != null) {
resourceAnnotationTypes.add(ejbClass);
}
}Beyond explicit dependencies, the @DependsOn annotation can be used to enforce a loading order for beans that are only implicitly dependent. By declaring @DependsOn on a bean (typically together with @Component or @Bean ), Spring ensures that the specified beans are instantiated first, which is useful in scenarios such as DDD‑driven designs or event‑listener based workflows.
The actual bean retrieval logic that respects @DependsOn can be seen in AbstractBeanFactory#doGetBean , where the factory checks the dependency metadata before creating the target bean:
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBeanIn summary, Spring provides two main mechanisms to control bean instantiation order: (1) explicitly declare dependencies with @Autowired , @Resource , or @Inject ; and (2) use @DependsOn to prioritize implicit dependencies.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.