03.SpringMVC of the device

The overall structure Introduction

In the Servlet inheritance structure in a total of five classes, GenericServlet HttpServlet and the rest of java in three classes HttpServletBean, FrameworkServlet and DispatcherServlet is SpringMVC in three classes directly implement three interfaces: EnvironmentCapable, EnvironmentAware and ApplicationContextAware.
XXXAware told XXX can be perceived, popular explanation is that point in the spring: If you want to use something like a spring in the inside, you can tell spring by implementing XXXAware interfaces, spring will see after you get over, and received by way of implementation of the interface is the only way setXXX, for example, there is a class you want to use the current ApplicationContext, then we just need to get it on ApplicationContextAware interface, and then implement the interface in a unique way, void setApplicationContext (ApplicationContext applicationContext (automatically pass over, directly)) on it, spring will automatically call this method will applicationContext to us, we only need to receive it.
EnvironmentCapable, as the name implies, of course, is the ability to have the Environment, which is to provide Environment, so the only way is EnvironmentCapable Environment getEnvironment (), for EnvironmentCapable class implements an interface, it can offer is to tell spring Environment, when spring is required Environment it will call its method it is saying to getEnvironment

HttpServletBean

HttpServletBean init method, the first parameter Servlet configured to set the BeanWrapper DispatcherServlet related properties, and then calls the template method initServletBean, subclass initialized by this method

public  Final  void the init () throws ServletException {
     // 1. Operation attributes configuration file 
    the PropertyValues PVS = new new ServletConfigPropertyValues (getServletConfig (), the this .requiredProperties); 
 
    IF (! pvs.isEmpty ()) {
         the try {
             // 2. Get target audience beanwrapper 
            the beanWrapper PropertyAccessorFactory.forBeanPropertyAccess BW = ( the this ); 
            the ResourceLoader ResourceLoader = new new ServletContextResourceLoader (GetServletContext ()); 
            bw.registerCustomEditor (the Resource. class, New new ResourceEditor (ResourceLoader, getEnvironment ()));
             // empty implementation 
            initBeanWrapper (BW); 
            bw.setPropertyValues (PVS, to true ); 
            } the catch (BeansException EX) {
                 the throw EX; 
            } 
           } 
    // empty method allow subclasses to achieve 
    initServletBean (); 
}

Spring BeanWrapper is provided a tool for operating JavaBean properties, which can be used to modify the properties of an object directly

public class User{
    String userName;
    //省略get和set方法
}
public class BeanWrapperTest{
    psvm{
        User user = new User();
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(user);
        bw.setPropertyValue("userName","张三");
        sout(user.getUserName());
        PropertyValue value = new PropertyValue("userName","李四");
        sout(user.getUserName());
    }
}

FrameworkServlet

FrameworkServlet inheritance HttpServletBean, FrameworkServlet initialization entry method is initServletBean

// org.springframework.web.servlet.FrameworkServlet 
protected  Final  void initServletBean () throws ServletException {
     // initialize the WebApplicationContext 
    the this .webApplicationContext = initWebApplicationContext ();
     // template method, which may be overlaid subclasses do some initialization work 
    initFrameworkServlet ( ); 
} 

protected the WebApplicationContext initWebApplicationContext () {
     // Get rootContext 
    the WebApplicationContext rootContext = WebApplicationContextUtils.getApplictionContext (GetServletContext ()); 
    the WebApplicationContext WAC = null ;
     //如果已经通过构造方法设置了webApplicationContext
    if(this.webApplicationContext != null){
        wac = this.webApplicationContext;
        if(wac instanceof ConfigurableWebApplicationContext){
            ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext ) wac;
            if(!cwac.isActive()){
                if(cwac.getParent() == null){
                    cwac.setParent(rootContext);
                }
                configureAndRefreshWebApplicationContext(cwac)
            } 
        } 
    }
    if(WAC == null ) {
         // when the ServletContext webApplicationContext already exists in the configuration acquired by the Servlet contextAttribute parameters 
        WAC = findWebApplicationContext (); 
    } 
    IF (WAC == null ) [
         // If webApplicationContext has not been created, then create a 
        WAC = createWebApplicationContext (rootContext); 
    } 
    iF (! the this .refreshEventReceived) {
         // this method, template method calls, can be overridden in a subclass when ContextRefreshedEvent event did not trigger 
        OnRefresh (WAC); 
    } 
    iF ( the this .publicContext ) {
         //将ApplicationContext保存到ServletContext中
        String attrName = getServletContextAttributeName();
        getServletContext().setAttribute(attrName,wac);
    }
    return wac;
}

This method does three things initWebApplicationContext

1. Obtain the root container rootContext spring

Gets the root container of principle is, by default, spring will own container ServletContext attribute set to the default key root container is org.springframework.web.context.WebApplicationContext.ROOT, so get the root container only needs to call on getAttribute ServletContext it

2. Set webApplicationContext and calls for the situation onRefresh
3. webApplicationContext disposed in the ServletContext

DispatcherServlet

OnRefresh DispathcerServlet method is the inlet, OnRefresh simply call initStrategis, nine calls the initialization method of the initStrategies

//org.springframework.web.servlet.DispatcherServlet
protected void onRefresh (ApplicationContext context){
    initStrategies(context);
}

protected void initStrategies(Application context){
    initMultipartResolver(context);
    initLocalResolver(context);
    initThemeResolver(context);
    initHandlerMappings(context);
    initHandlerAdapters(context);
    initHandlerExceptionResolvers(context);
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);
    initFlashMapManager(context);
}

 

Guess you like

Origin www.cnblogs.com/deityjian/p/11494345.html