Springboot는 외부 세계에 웹 서비스를 제공합니다. 맨 아래 계층은 Spring 프레임워크(Spring MVC 코어 클래스 DispatcherServlet을 포함하되 이에 국한되지 않음)의 웹 모듈을 사용하여 구현
springboot는 핵심 클래스 DispatcherServlet을 컨테이너에 주입합니까? 주입 프로세스는 여전히 자동 어셈블리 프로세스를 따릅니다. springboot 프레임워크는 기본적으로 spring.factories에 org.springframework.boot.autoconfigure.web.servlet을 제공합니다. .DispatcherServletAutoConfiguration 구성
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass(DispatcherServlet.class) @AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class) public class DispatcherServletAutoConfiguration { /* * The bean name for a DispatcherServlet that will be mapped to the root URL "/" */ public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet"; /* * The bean name for a ServletRegistrationBean for the DispatcherServlet "/" * public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration"; // ... 省略代码 }
DispatcherServletAutoConfiguration에는 DispatcherServletConfiguration, DispatcherServletRegistrationConfiguration 및 해당 조건 클래스 두 개가 있습니다. guration
DispatcherServletConfiguration은 DispatcherServletAutoConfiguration의 내부 클래스입니다. @ 구성 주석은 컨테이너에 의해 자동으로 스캔됩니다. 1. dispatcherServlet; 2. multipartResolver@Configuration(proxyBeanMethods = false) @Conditional(DefaultDispatcherServletCondition.class) @ConditionalOnClass(ServletRegistration.class) @EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class }) protected static class DispatcherServletConfiguration { // @A @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; } // @B @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; } }
DispatcherServletReg istrationConfiguration
@Configuration(proxyBeanMethods = false) @Conditional(DispatcherServletRegistrationCondition.class) @ConditionalOnClass(ServletRegistration.class) @EnableConfigurationProperties(WebMvcProperties.class) @Import(DispatcherServletConfiguration.class) //@A protected static class DispatcherServletRegistrationConfiguration { @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; } }
그 중 DispatcherServletRegistrationBean은 상속합니다. ServletRegistrationBean에서 왔으며 주로 DispatcherServlet에 대한 서비스를 제공합니다. DispatcherServletRegistrationBean과 DispatcherServlet 모두 Servlet을 등록하고 DispatcherServletPath 정보를 노출하는 기능을 제공합니다
위 내용은 DispatcherServlet을 주입하여 Springboot 자동 어셈블리를 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!