springboot mvc source resolution (a) automatic configuration and DispatcherServletRegistry DispatcherServlet

All articles

https://www.cnblogs.com/lay2017/p/11775787.html

 

text

springboot autoconfiguration SPI-based mechanism to achieve the core elements of automatic configuration is to add a class configured automatically, SpringBoot MVC automatic configuration is the same natural principle.

Beginning of this article, we will discuss Springboot under the Servlet web implementations. So, first find the corresponding auto-configuration class.

org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration

 

DispatcherServletAutoConfiguration automatic configuration class

We open class

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {
    //...
}

1, first noted, @ Configuration table name This is a configuration class, will spring to be resolved.

2, @ ConditionalOnWebApplication means that when a web project, and the project is Servlet time will be resolved.

3, @ ConditionalOnClass indicate DispatcherServlet the core class must exist only to resolve the class.

4, @ AutoConfigureAfter indicates re-analyzing ServletWebServerFactoryAutoConfiguration after this class, a set sequence.

Collectively, these notes show that the pre-conditions for the automatic configuration class will parse the need to meet.

 

Secondly, DispatcherServletAutoConfiguration class comprises two internal main categories, namely

1、DispatcherServletConfiguration

2、DispatcherServletRegistrationConfiguration

As the name suggests, the former is configured DispatcherServlet, which is a registered class configuration of DispatcherServlet. What is a registered class? We know Servlet examples are to be added (registered) to tomcat as such in the ServletContext, so as to provide the requested service. So, DispatcherServletRegistrationConfiguration will generate a Bean, responsible DispatcherServlet to register in the ServletContext.

 

配置DispatcherServletConfiguration

We take a look at the configuration class DispatcherServletConfiguration

@Configuration(proxyBeanMethods = false)
@Conditional(DefaultDispatcherServletCondition.class)
@ConditionalOnClass(ServletRegistration.class)
@EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class })
protected static class DispatcherServletConfiguration {

    //...
}

@Conditional indicates a pre-determined condition, achieved by the DefaultDispatcherServletCondition. The main is to determine if there's already DispatcherServlet, will trigger if there is no resolution.

@ConditionalOnClass indicates when ServletRegistration this class exists to trigger the resolution, resulting DispatcherServlet to register in the ServletContext.

Finally, @ EnableConfigrationProperties spring.http will read attribute and generates the configuration spring.mvc prefix objects and WebMvcProperties HttpProperties application.properties from this profile.

 

This inner class look DispatcherServletConfiguration internal code

@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvcProperties webMvcProperties) {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
    dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
    dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
    dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
    return dispatcherServlet;
}

@Bean
@ConditionalOnBean(MultipartResolver.class)
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
public MultipartResolver multipartResolver(MultipartResolver resolver) {
    // Detect if the user has created a MultipartResolver but named it incorrectly
    return resolver;
}

The two methods we are more familiar with, is to generate the Bean.

dispatcherServlet method will generate a DispatcherServlet of Bean object. Relatively simple, it is to get an instance, and then add some attribute settings.

multipartResolver main method is to MultipartResolver of Bean your configuration to rename it, preventing you do not use multipartResolver this name as the name of Bean.

 

配置DispatcherServletRegistrationConfiguration

Look at the Bean class configuration register

@Configuration(proxyBeanMethods = false)
@Conditional(DispatcherServletRegistrationCondition.class)
@ConditionalOnClass(ServletRegistration.class)
@EnableConfigurationProperties(WebMvcProperties.class)
@Import(DispatcherServletConfiguration.class)
protected static class DispatcherServletRegistrationConfiguration {
    //...
}

The same, @ Conditional there is a pre-judgment, DispatcherServletRegistrationCondition mainly determine the presence or absence of a registered class of Bean.

@ConditionOnClass also determine whether there is ServletRegistration

@EnableConfigurationProperties the generated object attributes WebMvcProperties

@Import introduced DispatcherServletConfiguration, that is, we the above configuration object.

 

Look DispatcherServletRegistrationConfiguration internal implementation

@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
        WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
    DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
            webMvcProperties.getServlet().getPath());
    registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
    registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
    multipartConfig.ifAvailable(registration::setMultipartConfig);
    return registration;
}

Internal only one way to generate DispatcherServletRegistrationBean. Core logic that instantiates a Bean, set some parameters, such as dispatcherServlet, loadOnStartup like.

 

to sum up

springboot mvc automatic configuration class is DispatcherServletAutoConfigration, mainly do two things:

1) Configuration DispatcherServlet

2) Configuration DispatcherServlet registration Bean

 

Guess you like

Origin www.cnblogs.com/lay2017/p/11803794.html
Recommended