Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Controlling Bean Initialization Order in Spring: Using @Autowired, @Resource, @Inject and @DependsOn

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.
 * <p>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<? extends Annotation>)
            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#doGetBean

In 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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaSpringDependency InjectionBean Initialization

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion