1.springboot自动默认扫描路径:
启动类的包下及其子包。
2.springboot自动配置原理:
1.从所以依赖中找META-INF/spring.factories文件,默认加载文件中写的所有XXXAutoConfiguration。
springboot默认的自动配置场景有127个*****
2.虽然加载了这么多个自动配置类,但不是所以自动配置类都能生效。springboot为我们想到了这么多个场景,但具体让谁生效还是根据我们的需求来生效。因为XXXAutoCongratulation类中大量使用@ConditionalOnBean @,@ConditionalOnClass,@ConditionalOnMissingBean,@ConditionalOnMissingClass等等按要求生效的注解。所以很多时候只有我们真正自己导入相关依赖时这些某些配置才会生效。
例如DispatcherServletAutoConfiguration肯定是生效的。
@AutoConfigureOrder(-2147483648)
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({DispatcherServlet.class})
@AutoConfigureAfter({ServletWebServerFactoryAutoConfiguration.class})
public class DispatcherServletAutoConfiguration {
.....}
3.XXXAutoConfiguration生效后,会给容器装配组件。有了这些组件,就等于有了某些功能。
例如DispatcherServletAutoConfiguration类的内部类。在里面装配了DispatcherServlet
@Configuration(
proxyBeanMethods = false
)
@Conditional({DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition.class})
@ConditionalOnClass({ServletRegistration.class})
@EnableConfigurationProperties({HttpProperties.class, WebMvcProperties.class})
protected static class DispatcherServletConfiguration {
protected DispatcherServletConfiguration() {
}
@Bean(
name = {"dispatcherServlet"}
)
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;
}
4.XXXAutoConfiguration多数时候和XXXProperties联用,@EnableConfigurationProperties+@ConfigurationProperties() 比如@EnableConfigurationProperties({WebMvcProperties.class})在配置类中加入这段代码,就会让容器中加入一个WebMvcProperties.class组件,并且读取springboot配置文件对该类的属性进行赋值。
@ConfigurationProperties(
prefix = "spring.mvc"
)
public class WebMvcProperties
当我们不想用springboot默认的属性,可以在springboot配置文件中以指定的前缀+属性名修改对应属性。
5.当我们不想用springboot为我们创建的bean时也可以自己在配置文件中创建bean。因为springboot默认优先使用我们自己写的bean。底层大量使用@ConditionalOnMissingBean。
3. Web开发定制化
Web应用 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能;+ @Bean给容器中再扩展一些组件
public interface WebMvcConfigurer {
default void configurePathMatch(PathMatchConfigurer configurer) {
}
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
}
default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
default void addFormatters(FormatterRegistry registry) {
}
default void addInterceptors(InterceptorRegistry registry) {
}
default void addResourceHandlers(ResourceHandlerRegistry registry) {
}
default void addCorsMappings(CorsRegistry registry) {
}
default void addViewControllers(ViewControllerRegistry registry) {
}
default void configureViewResolvers(ViewResolverRegistry registry) {
}
default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
}
default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
}
default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
}
default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
}
default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
}
default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
}
@Nullable
default Validator getValidator() {
return null;
}
@Nullable
default MessageCodesResolver getMessageCodesResolver() {
return null;
}