Backend Development 6 min read

Three Ways to Access Spring Beans in Static Methods Using Spring Boot

This article explains three techniques for obtaining Spring beans inside static methods—using @PostConstruct initialization, a static ApplicationContext stored in the main class, and implementing ApplicationContextAware—complete with code examples and detailed guidelines for the @PostConstruct annotation.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Three Ways to Access Spring Beans in Static Methods Using Spring Boot

This article demonstrates three practical approaches to retrieve Spring beans from static methods in a Spring Boot application.

Method 1 – @PostConstruct initialization : A component injects the required bean via @Autowired, then assigns it to a static field inside a @PostConstruct method, allowing static methods to use the bean.

import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
 * springboot静态方法获取 bean 的三种方式(一)
 * @author: clx
 * @version: 1.1.0
 */
@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 annotation must follow several rules: the method cannot have parameters (except in EJB interceptors), must return void, cannot throw checked exceptions, may have any visibility, must not be static, and only one method per class may be annotated.

The method must have no parameters unless used as an EJB interceptor.

The return type must be void .

The method must not throw checked exceptions.

Visibility can be public, protected, package‑private, or private.

The method cannot be static (except for client‑side code).

Only one method per class may be annotated with @PostConstruct .

The method may be final .

Method 2 – Storing ApplicationContext in a static field : The Spring Boot entry class defines a static ConfigurableApplicationContext variable that is populated in main . Other classes retrieve beans via this static context.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
 * @author: clx
 * @version: 1.1.0
 */
@SpringBootApplication
public class Application {
    public static ConfigurableApplicationContext ac;
    public static void main(String[] args) {
        ac = SpringApplication.run(Application.class, args);
    }
}

Usage in a controller:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author: clx
 * @version: 1.1.0
 */
@RestController
public class TestController {
    @GetMapping("test2")
    public void method_2() {
        AutoMethodDemoService methodDemoService = Application.ac.getBean(AutoMethodDemoService.class);
        String test2 = methodDemoService.test2();
        System.out.println(test2);
    }
}

Method 3 – Implementing ApplicationContextAware : A component implements ApplicationContextAware to capture the context in a static variable, then provides a generic getBean method.

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 * springboot静态方法获取 bean 的三种方式(三)
 * @author: clx
 * @version: 1.1.0
 */
@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;
    }
}

Calling the generic getter:

/**
 * 方式三
 */
@Test
public void method_3() {
    AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
    String test3 = autoMethodDemoService.test3();
    System.out.println(test3);
}

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

JavaSpring Bootdependency injectionstatic methodsApplicationContextPostConstruct
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow 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.