Spring based context tool class ApplicationContextUtil

  • 2021-12-12 08:50:00
  • OfStack

Directory Spring Context Tool Class ApplicationContextUtil Get ApplicationContext Tool Class Mode 1 Mode 2 Mode 3 Mode 4 Mode 5

Spring Context Tool Class ApplicationContextUtil


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
/**
 * spring  Context tool class 
 * @author DAWN
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
 
    /**
     *  Context object instance 
     */
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }
 
    /**
     *  Get applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    /**
     *  Pass name Get  Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
 
    /**
     *  Pass class Get Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
 
    /**
     *  Pass name, As well as Clazz Returns the specified Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

Get the tool class for ApplicationContext

In projects, we often encounter such problems:

Some classes need to use new to create objects, but the class needs to use bean defined in the spring container, so it is impossible to inject bean that we need to use through automatic injection of spring.

Therefore, it is necessary to manually obtain bean from spring container. To get an bean, you must first get an ApplicationContext object, which can be obtained in the following ways.

Mode 1

Manually create an ApplicationContext object and save it.


public class ApplicationContextUtil {
    private static ApplicationContext context;
    static {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    public static ApplicationContext getApplicationContext() {
        return context;
    }
}

Mode 2

Obtained in an web environment through a tool class provided by spring, requiring an ServletContext object as a parameter. bean is then obtained through the ApplicationContext object.

The difference between the following two tools is that the former returns null when the fetch fails, while the latter throws an exception.

In addition, since spring is the container object placed in ServletContext, the WebApplicationContext object can be fetched directly in ServletContext.


ApplicationContext context1 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ApplicationContext context2 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

Mode 3

The tool class inherits the abstract class ApplicationObjectSupport and is managed by spring using @ Component on the tool class.

Thus, when the spring container is started, the ApplicationContext object is set in through the setApplicationContext () method in the parent class ApplicationObjectSupport.

The ApplicationContext object can be obtained through getApplicationContext ().

Mode 4

Tool class inheritance abstract class WebApplicationObjectSupport, see the source code WebApplicationObjectSupport is inherited from ApplicationObjectSupport, so get ApplicationContext object and the above 1, is also using getApplicationContext () method.

Mode 5

The tool class implements the ApplicationContextAware interface and overrides the setApplicationContext (ApplicationContext applicationContext) method, using the @ Component annotation in the tool class for spring to manage.

When the spring container is started, the setApplicationContext () method is called to set the ApplicationContext object in.


@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return context;
    }
}

Related articles: