
Author | chilx
Source| https://blog.csdn.net/showchi/article/details/97005720
Note: callers are managed by spring
Mode 1
Annotation @ PostConstruct
import com.example.javautilsproject.service.AutoMethodDemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; /** * springboot Three ways to obtain bean s by static methods (1) * @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 is used on the methods that need to be executed after dependency injection to perform any initialization. This method must be called before the class is placed into the service.
All classes that support dependency injection must support this annotation. Methods annotated with PostConstruct must be called even if the class does not request any resources to be injected. Only one method can be annotated with this annotation.
If you are learning Spring Boot, we recommend a free tutorial that has been continuously updated for many years: http://blog.didispace.com/spring-boot-learning-2x/
The method of applying PostConstruct annotations must comply with all of the following standards:
- The method must not have any parameters, except in the case of EJB interceptor, according to the definition of EJB specification, in which case it will have an InvocationContext object;
- The return type of this method must be void;
- The method shall not throw checked exceptions;
- The method of applying PostConstruct can be public, protected, package private or private;
- Except for the application client, the method cannot be static;
- The method can be final;
- If the method throws an unchecked exception, you must not put the class into the service unless it is an EJB that can handle the exception and recover from it.
- Spring Boot learning notes, this is too complete!
Mode II
Start class ApplicationContext
Implementation method: in the startup class of springboot, define the static variable ApplicationContext, and use the container's getBean method to obtain the dependent object.
If you are learning Spring Boot, we recommend a free tutorial that has been continuously updated for many years: http://blog.didispace.com/spring-boot-learning-2x/
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); } }
Call mode
/** * @author: clx * @version: 1.1.0 */ @RestController public class TestController { /** * Mode II */ @GetMapping("test2") public void method_2() { AutoMethodDemoService methodDemoService = Application.ac.getBean(AutoMethodDemoService.class); String test2 = methodDemoService.test2(); System.out.println(test2); } }
Mode III
Manually inject ApplicationContext
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * springboot Three ways to obtain bean s by static methods (3) * @author: clx * @version: 1.1.0 */ @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; } }
In addition, if you are learning Spring Cloud, we recommend a free tutorial that has been continuously updated for many years: https://blog.didispace.com/spring-cloud-learning/
Call mode
/** * Mode III */ @Test public void method_3() { AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class); String test3 = autoMethodDemoService.test3(); System.out.println(test3); }
The above three methods have been tested and can be used perfectly.
(end)