In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the ways for SpringBoot static methods to obtain bean". In daily operation, I believe that many people have doubts about the way SpringBoot static methods obtain bean. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the ways of SpringBoot static methods to obtain bean?" Next, please follow the editor to study!
Method one Note @ PostConstruct
Three ways for 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 to obtain bean (1) * @ author: clx * @ date: 2019-7-23 * @ version: 1.1.0 * / @ Componentpublic class StaticMethodGetBean_1 {@ Autowired private AutoMethodDemoService autoMethodDemoService; @ Autowired private staticAutoMethodDemoService staticAutoMethodDemoService @ PostConstruct public void init () {staticAutoMethodDemoService = autoMethodDemoService;} public static String getAuthorizer () {return staticAutoMethodDemoService.test ()}
Note @ PostConstruct description
The PostConstruct annotation is used to perform any initialization on methods that need to be executed after dependency injection is complete. This method must be called before putting the class 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 resource injection. There is only one way to comment with this comment.
The method of applying PostConstruct annotations must comply with all of the following standards:
This method must not have any parameters, except in the case of an EJB interceptor (interceptor), in which case it will have an InvocationContext object as defined by the EJB specification
The return type of this method must be void
This method must not throw a checked exception
The method of applying PostConstruct can be public, protected, package private or private
This method cannot be static except for the application client
This method can be final
If the method throws an unchecked exception, the class must not be put into the service unless it is an EJB that can handle the exception and recover from it.
Mode 2 starts the class ApplicationContext
Implementation: in the startup class of springboot, define the static variable ApplicationContext, and use the getBean method of the container to obtain the dependent object
Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;/** * @ author: clx * @ date: 2019-7-23 * @ version: 1.1.0 * / @ SpringBootApplicationpublic class Application {public static ConfigurableApplicationContext ac; public static void main (String [] args) {ac = SpringApplication.run (Application.class, args);}}
Calling mode
/ * * @ author: clx * @ date: 2019-7-23 * @ version: 1.1.0 * / @ RestControllerpublic class TestController {/ * method 2 * / @ GetMapping ("test2") public void method_2 () {AutoMethodDemoService methodDemoService = Application.ac.getBean (AutoMethodDemoService.class); String test2 = methodDemoService.test2 (); System.out.println (test2);} method 3 manually inject ApplicationContext
Manual injection of ApplicationContext
Three ways for import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; / * springboot static method to obtain bean (3) * @ author: clx * @ date: 2019-7-23 * @ version: 1.1.0 * / @ Componentpublic 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 mode
/ * * method 3 * / @ Test public void method_3 () {AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean (AutoMethodDemoService.class); String test3 = autoMethodDemoService.test3 (); System.out.println (test3);} at this point, the study of "what are the ways for SpringBoot static methods to obtain bean" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.