Three Ways to Access Spring Beans from Static Methods
This article demonstrates three practical techniques for retrieving Spring-managed beans inside static methods—using @PostConstruct initialization, a static ApplicationContext from the main class, and a manual ApplicationContextAware component—complete with code examples and usage guidelines.
Note: The caller must be managed by Spring.
Method One
@PostConstruct Annotation
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 annotation marks a method that runs after dependency injection is complete, allowing initialization logic such as assigning a bean to a static field.
All classes that support dependency injection must honor this annotation; even if a class does not request any resources, a method annotated with @PostConstruct must still be invoked. Only one method per class may be annotated.
The method must have no parameters (except in EJB interceptor cases).
The return type must be void.
The method must not throw checked exceptions.
It may be public, protected, package‑private, or private.
It must not be static (except for client applications).
It may be final.
If it throws unchecked exceptions, the class cannot be placed in the container unless the exception can be handled and recovered.
Method Two
Static ApplicationContext in the Startup Class
Define a static ApplicationContext field in the Spring Boot main class and obtain beans via getBean.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* springboot static method bean retrieval (2)
*/
@SpringBootApplication
public class Application {
public static ConfigurableApplicationContext ac;
public static void main(String[] args) {
ac = SpringApplication.run(Application.class, args);
}
}Usage example:
@RestController
public class TestController {
/**
* Method Two
*/
@GetMapping("test2")
public void method_2() {
AutoMethodDemoService service = Application.ac.getBean(AutoMethodDemoService.class);
String result = service.test2();
System.out.println(result);
}
}Method Three
Manual ApplicationContext Injection
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* springboot static method bean retrieval (3)
*/
@Component
public class StaticMethodGetBean_3<T> implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
StaticMethodGetBean_3.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext != null ? applicationContext.getBean(clazz) : null;
}
}Usage example:
@Test
public void method_3() {
AutoMethodDemoService service = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
String result = service.test3();
System.out.println(result);
}All three approaches have been tested and work perfectly.
(End)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
