IDE: IntelliJ IDEA 2022.2.3 x64
Operating system: win10 x64 bit home edition
JDK: 1.8
Article directory
- Preface
- 1. How to manually obtain the spring container [ApplicationContext]?
-
- Method ①: Get the spring container in the startup class
- Method ②: Customize the tool class to implement the ServletContextListener interface to obtain the spring container [recommended web project]
- Method ③: Customize the tool class to implement the ApplicationContextAware interface to obtain the spring container
- Method ④: Custom tool class inherits the ApplicationObjectSupport abstract class to obtain the spring container
- Method ⑤: Custom tool class inherits the WebApplicationObjectSupport abstract class to obtain the spring container
- References
Foreword
As we all know, the global context object in the Spring framework is usually called ApplicationContext. This is a manifestation of the Spring container, which is responsible for managing and controlling the objects (Beans) in the application. It essentially maintains the definition of Bean and the collaborative relationship between objects. When the project is running, the ApplicationContext will automatically load all the information in the Handler into memory. In addition, Spring context is also a configuration file that can provide context information to the Spring framework. Therefore, we can say that ApplicationContext is the core part of the Spring framework and provides various convenient services to help application development.
Tips: The following is the text of this article, the following cases are for reference
1. How to manually obtain the spring container [ApplicationContext]?
Method 1: Get the spring container in the startup class
Get the spring container in the startup class
The code example is as follows
@SpringBootApplication public class UserApplication {<!-- --> //Get the spring container object when the startup class is running public static ConfigurableApplicationContext context; public static void main(String[] args) {<!-- --> context = SpringApplication.run(UserApplication.class,args); } }
Call the spring container obtained in the startup class in the custom Controller class
@GetMapping("/sayhi1") public String sayhi1(){<!-- --> //Get the Myservice bean from the spring container, and then call the sayHi1() method of the bean MyService myService = UserApplication.context.getBean(MyService.class); return myService.sayHi(); }
Test
Method 2: Customize the tool class to implement the ServletContextListener interface to obtain the spring container [recommended web project]
Custom tool class implements ServletContextListener interface
The sample code is as follows
import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener @Component public class MyWebApplicationContext implements ServletContextListener{<!-- --> private ServletContext servletcontext; @Override public void contextInitialized(ServletContextEvent sce) {<!-- --> this.servletcontext = sce.getServletContext(); } public ApplicationContext getApplicationcontext() {<!-- --> return WebApplicationContextUtils.getWebApplicationContext(this.servletcontext); } }
Inject the tool class written above into the custom Controller class, and then obtain the spring container
@Autowired private MyWebApplicationContext myWebApplicationContext; @GetMapping("/sayhi2") public String sayhi2(){<!-- --> //Use the tool class to get the spring container, then get the myService bean from it, and call the sayHi() method in it MyService myService = myWebApplicationContext.getApplicationcontext().getBean(MyService.class); return myService.sayHi(); }
Test
Method ③: Customize the tool class to implement the ApplicationContextAware interface to obtain the spring container
Custom tool class implements the ApplicationContextAware interface
The sample code is as follows
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class MyApplicationAware implements ApplicationContextAware {<!-- --> private ApplicationContext applicationcontext; @Override public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException {<!-- --> this.applicationcontext = applicationcontext; } public ApplicationContext getApplicationcontext() {<!-- --> return this.applicationcontext; } }
Inject the tool class written above into the custom Controller class, and then obtain the spring container
@Autowired private MyApplicationAware myApplicationAware; @GetMapping("/sayhi3") public String sayhi3(){<!-- --> //Use the tool class to get the spring container, then get the myService bean from it, and call the sayHi() method in it MyService myService = myApplicationAware.getApplicationcontext().getBean(MyService.class); return myService.sayHi(); }
Test
Method ④: Custom tool class inherits the ApplicationObjectSupport abstract class to obtain the spring container
The custom tool class inherits the ApplicationObjectSupport abstract class
The code example is as follows
import org.springframework.context.support.ApplicationObjectSupport; import org.springframework.stereotype.Component; @Component public class MyApplicationObjectSupport extends ApplicationObjectSupport {<!-- --> }
Inject the tool class written above into the custom Controller class, and then obtain the spring container
@Autowired private MyApplicationObjectSupport myApplicationObjectSupport; \t @GetMapping("/sayhi4") public String sayhi4(){<!-- --> //Use the tool class to get the spring container, then get the myService bean from it, and call the sayHi() method in it MyService myService = myApplicationObjectSupport.getApplicationContext().getBean(MyService.class); return myService.sayHi(); }
Test
Method 5: Custom tool class inherits the WebApplicationObjectSupport abstract class to obtain the spring container
The custom tool class inherits the WebApplicationObjectSupport abstract class
The code example is as follows
import org.springframework.stereotype.Component; import org.springframework.web.context.support.WebApplicationObjectSupport; @Component public class MyWebApplicationObjectSupport extends WebApplicationObjectSupport {<!-- --> }
Inject the tool class written above into the custom Controller class, and then obtain the spring container
@Autowired private MyWebApplicationObjectSupport myWebApplicationObjectSupport; \t @GetMapping("/sayhi5") public String sayhi5(){<!-- --> //Use the tool class to get the spring container, then get the myService bean from it, and call the sayHi() method in it MyService myService = myWebApplicationObjectSupport.getApplicationContext().getBean(MyService.class); return myService.sayHi(); }
Test
Reference materials
https://www.bilibili.com/video/BV1QP4y137Tz/?spm_id_from=333.788.top_right_bar_window_history.content.click & amp;vd_source=5a34715e416a427a73a3ca52397848b5