There are several ways to get Spring's ApplicationContext

Java class to get spring container bean

 

Summary of the five commonly used ways to obtain beans in spring:

 

Method 1: Save the ApplicationContext object during initialization

Code:

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");

ac.getBean("beanId");

Note: This method is suitable for independent applications using the Spring framework, and the program needs to manually initialize Spring through the configuration file.

 

Method 2: Obtain the ApplicationContext object through the tool class provided by Spring

Code:

import org.springframework.web.context.support.WebApplicationContextUtils;

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);

ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);

ac1.getBean("beanId");

ac2.getBean("beanId");

illustrate:

This method is suitable for the B/S system that adopts the Spring framework, obtains the ApplicationContext object through the ServletContext object, and then obtains the required class instance through it.

 

The difference between the above two tools is that the former throws an exception when the acquisition fails, and the latter returns null.

 

Method 3: Inherit from the abstract class ApplicationObjectSupport

Description: The abstract class ApplicationObjectSupport provides the getApplicationContext() method, which can easily obtain the ApplicationContext.

When Spring is initialized, the ApplicationContext object will be injected through the setApplicationContext(ApplicationContext context) method of the abstract class.

 

Method 4: Inherit from the abstract class WebApplicationObjectSupport

Description: Similar to the above method, call getWebApplicationContext() to get the WebApplicationContext

 

Method 5: Implement the interface ApplicationContextAware

Description: Implement the setApplicationContext(ApplicationContext context) method of this interface and save the ApplicationContext object.

When Spring is initialized, it will inject the ApplicationContext object through this method.

 

 

 

Although spring provides the last three methods to inherit or implement corresponding classes or interfaces in ordinary classes to obtain Spring's ApplicationContext objects, it is necessary to pay attention to ordinary java classes that implement these classes or interfaces when using them. Configure in Spring's configuration file application-context.xml file. Otherwise the obtained ApplicationContext object will be null.

 

 

 

The following is an example of my implementation of the ApplicationContextAware interface

 

package quartz.util;

 

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

 

public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{

 

private static ApplicationContext context = null;

private static SpringConfigTool stools = null;

public synchronized static SpringConfigTool init(){

  if(stools == null){

   stools = new SpringConfigTool();

  }

  return stools;

}

 

public void setApplicationContext(ApplicationContext applicationContext)

throws BeansException {

  context = applicationContext;

}

 

public synchronized static Object getBean(String beanName) {

  return context.getBean (beanName);

}

 

}

 

Configuration information in XML files

 

Finally, provide a way that does not depend on servlet and does not require injection

Note that when the server starts, when the Spring container is initialized, the Spring container cannot be obtained through the following methods. For details, you can view the source code org.springframework.web.context.ContextLoader

 

Title1 import org.springframework.web.context.ContextLoader; 

import org.springframework.web.context.WebApplicationContext; 

 

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 

  wac.getBean(beanID); 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326953735&siteId=291194637