How does SpringBoot use ApplicationContext to get bean objects
Editor to share with you how SpringBoot uses ApplicationContext to get bean objects. I hope you will get something after reading this article. Let's discuss it together.
Use ApplicationContext to get the bean object
Write an ApplicationContextFactory factory class
Public class ApplicationContextFactory {private static ApplicationContext applicationContext = null; public static void setApplicationContext (ApplicationContext applicationContext) throws BeansException {applicationContext = applicationContext;} public static ApplicationContext getApplicationContext () {return applicationContext;}}
Set ApplicationContext in the startup class of SpringBoot
Public class Application {public static void main (String [] args) {ConfigurableApplicationContext app = SpringApplication.run (Application.class, args); ApplicationContextFactory.setApplicationContext (app);}}
Get SpringApplication through ApplicationContextFactory to get bean object
ApplicationContext applicationContext=ApplicationContextFactory.getApplicationContext (); Clazz clazz = applicationContext.getBean (Clazz.class); in-depth study of SpringBoot Bean injection the following code works properly
DemoService
@ Servicepublic class DemoService {public void save () {System.out.println ("DemoService save");}}
CommonClass
Componentpublic class CommonClass {@ Resource private DemoService demoService; public void fun () {System.out.println ("fun"); demoService.save ();}}
Controller
@ Resourceprivate CommonClass commonClass;@ResponseBody@GetMapping ("/ fun") public void fun () {commonClass.fun ();} the following code does not work properly
DemoService
@ Servicepublic class DemoService {public void save () {System.out.println ("DemoService save");}}
CommonClass
Public class CommonClass {@ Resource private DemoService demoService; public void fun () {System.out.println ("fun"); demoService.save ();}}
Controller
@ ResponseBody@GetMapping ("/ fun") public void fun () {CommonClass commonClass = new CommonClass (); commonClass.fun ()} compare
Comparing the two code, the difference between the latter and the former is found: because the CommonClass of the latter does not use the @ Component annotation, the CommonClass object cannot be generated by injection in Controller, but by new.
In this way, the CommonClass object is created by hand, so the code that injects the DemoService object inside it is wrong.
Solution
Create a new tool class
@ Componentpublic class ApplicationContextUtil implements ApplicationContextAware {private static ApplicationContext act; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {act = applicationContext;} / * get the corresponding bean object in the factory according to the name of bean * @ param beanName * @ return * / public static Object getBean (String beanName) {return act.getBean (beanName);}}
Note: the actual test found that the static in the above code cannot be omitted.
DemoService
@ Servicepublic class DemoService {public void save () {System.out.println ("DemoService save");}}
CommonClass
Public class CommonClass {@ Resource private DemoService demoService; public void fun () {DemoService demoService = (DemoService) ApplicationContextUtil.getBean ("demoService"); System.out.println ("fun"); demoService.save ();}}
Instead of getting DemoService objects by injection, we use tool classes to get them.
Controller
ResponseBody@GetMapping ("/ fun") public void fun () {CommonClass commonClass = new CommonClass (); commonClass.fun ();}
Run the program again and everything is fine.
Application
In the case of SpringBoot integrating Shiro, you need to use Service's objects when customizing Realm. Because the custom Realm class cannot use annotations such as @ Component, the approach described in this case is the right solution. Although the following code works correctly in 1.6.0 shiro-all:
After reading this article, I believe you have a certain understanding of "how SpringBoot uses ApplicationContext to obtain bean objects". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!