This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

preface

Aware means to inject. Today we’ll take a look at two heavyweight attribute injection classes

servlet

  • The early days of Web projects were dominated by servlets. So the ServletContext has to be mentioned, and in web applications we’re familiar with the Request scope and the Session scope

The request scope

  • The life cycle of a request, from when the client creates the request to when the server responds to the request, is the generation cycle of a request.
  • Requests are very time-sensitive and are usually operations on word requests. The parameters and responses to the request are also one-time

Session scope

  • Out of request scope, sessions are for clients, and a client can be considered a session object for a certain amount of time. The duration can span multiple requests.
  • The usage scenario of the session is usually in the user login operation

ServletContext scope

  • For the ServletContext its scope is even higher, it overrides the server. Or its life cycle follows the server, starting the server is ServletContext, closing the server is ServletContext.
  • For ServletContext we actually use it a lot. In MVC, the request path of our interface is stored in the ServletContext for data sharing. Because the interface, once set up, does not change during the program’s run, other places need to get mapping information during the run through ServletContext. If you use a Session, then you have to design the client to keep the connection and so on.

contrast

  • Request, Session, and ServletContext all store attributes as key-value pairs
  • The life cycle of the three gradually expands
  • The ServletContext can be used to get properties built into the program through getInitParameter. Use getAttribute to get dynamically set attributes. You can use getResource to obtain resource file information
  • The ServletContext also has a place to store temporary files. Through the javax.mail. Servlet. Context. Tempdir context properties

To obtain

public interface ZxhtomComponent extends ApplicationContextAware.ServletContextAware
Copy the code
  • The ZxhtomComponent can then call getServletContext to get the ServletContext class to do the actual work.

spring

  • Sometimes we need to fetch Spring beans in a normal class while the program is running, and we can’t implement property injection through @Autowire annotations. Spring provides thatApplicationContext The specified bean can be obtained manually. But that property is an interface that we need to implement if we want to use itApplicationContextAwareThis interface. This interface ensures that theApplicationContextProperties into
public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext var1) throws BeansException;
}
Copy the code
  • We implement this interface and add the ApplicationContext attribute to the subclass
@Component  // It must be added here because the final callback is done by BeanPostProcessor. This class is intercepted before and after bean initialization
public class ApplicationContextUtil implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext(a) {
        return ApplicationContextUtil.applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        returngetApplicationContext().getBean(name, clazz); }}Copy the code
  • The generic class ApplicationContextUtil is now complete, and we can get the Beans in Spring anywhere we want through the class’s getBean method.

Aware

  • Above, we realized how to obtain the corresponding context class from two perspectives of servlet and Spring. ServletContext in servlets, ApplicationContext in Spring. They are all injected via the Aware ending class.
  • In other words, they all inherited org. Springframework. Beans. Factory. Aware interface. They are ApplicationContextAware and ServletContextAware.
  • The ownership of Aware is to inject beans into normal classes. Thus, relevant operations can be performed in context.

Other Related Aware

BeanNameAware

  • Gets the name of the bean in the container

BeanFactoryAware

  • Gets the current BeanFactory

MessageSourceAware

  • Get MessageSource, internationalized

ResourceLoaderAware

  • Get ResourceLoader

Where the injection

  • Springboot function entry in AbstractApplicationContext# refresh method. In this method we know we have a prepareBeanFactory

  • Then we go in and find that the most red invokeAwareInterfaces call the property of Aware below.