Backend Development 5 min read

Three Methods to Obtain Spring Beans in Static Context: @PostConstruct, ApplicationContext, and ApplicationContextAware

This article explains three practical ways to retrieve Spring beans from static methods—using @PostConstruct annotation, a static ApplicationContext defined in the main class, and a manually injected ApplicationContext via ApplicationContextAware—complete with code examples and usage notes.

Top Architect
Top Architect
Top Architect
Three Methods to Obtain Spring Beans in Static Context: @PostConstruct, ApplicationContext, and ApplicationContextAware

In Spring Boot applications, static methods cannot directly access beans managed by the container. The article presents three viable approaches to bridge this gap.

Method 1: Using @PostConstruct Annotation

A component class injects the required service via @Autowired, then assigns it to a static field inside a method annotated with @PostConstruct, ensuring the assignment occurs after dependency injection.

import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

/**
 * springboot static method bean retrieval (1)
 */
@Component
public class StaticMethodGetBean_1 {
    @Autowired
    private AutoMethodDemoService autoMethodDemoService;

    @Autowired
    private static AutoMethodDemoService staticAutoMethodDemoService;

    @PostConstruct
    public void init() {
        staticAutoMethodDemoService = autoMethodDemoService;
    }

    public static String getAuthorizer() {
        return staticAutoMethodDemoService.test();
    }
}

The @PostConstruct method must have no parameters, return void, and cannot be static; it runs after all injections are completed.

Method 2: Defining a Static ApplicationContext in the Startup Class

The main Spring Boot class declares a static ConfigurableApplicationContext field and assigns the context returned by SpringApplication.run . Beans can then be fetched via ac.getBean(...) .

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static ConfigurableApplicationContext ac;

    public static void main(String[] args) {
        ac = SpringApplication.run(Application.class, args);
    }
}

@RestController
public class TestController {
    @GetMapping("test2")
    public void method_2() {
        AutoMethodDemoService service = Application.ac.getBean(AutoMethodDemoService.class);
        System.out.println(service.test2());
    }
}

Method 3: Manually Injecting ApplicationContext via ApplicationContextAware

A component implements ApplicationContextAware to capture the context in a static field, providing a generic getBean(Class ) method for later static access.

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class StaticMethodGetBean_3
implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        StaticMethodGetBean_3.applicationContext = applicationContext;
    }

    public static
T getBean(Class
clazz) {
        return applicationContext != null ? applicationContext.getBean(clazz) : null;
    }
}

@Test
public void method_3() {
    AutoMethodDemoService service = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
    System.out.println(service.test3());
}

All three approaches have been tested and work reliably for accessing beans in static contexts.

JavaSpring Bootdependency injectionstatic methodApplicationContextPostConstructBean Retrieval
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.