@SpringBootApplication注解SpringBoot深度理解,理解代码原理要有深度。接下来简单的回顾上次课讲的的内容吧
第一部分回顾:SpringBoot自动配置流程:从上到下
@SpringBootApplication
@EnableAutoConfiguration
@Import({AutoConfigurationImportSelector.class})
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {}
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
public final class SpringFactoriesLoader {
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";}
Enumeration urls = classLoader.getResources("META-INF/spring.factories");
application.properties文件中能写什么呢?----联系---spring.factories有啥关系吗?
#比如我随便写一个 server.port=8087
//当我进入server.port文件中会有下面的提示 public void setPort(Integer port) { this.port = port; }
@ConfigurationProperties( prefix = "server", ignoreUnknownFields = true ) public class ServerProperties {}
好像也找不到关系呀!没有关系换个案例
egg1
#看一个spring.mvc.format.date-time spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
public void setDateTime(String dateTime) { this.dateTime = dateTime; }
import java.time.Duration; import java.util.LinkedHashMap; import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; import org.springframework.boot.context.properties.IncompatibleConfigurationException; import org.springframework.http.MediaType; import org.springframework.util.Assert; @ConfigurationProperties( prefix = "spring.mvc" ) public class WebMvcProperties {
egg2
#spring.mvc.static-path-pattern=/** spring.mvc.static-path-pattern=/**
public void setStaticPathPattern(String staticPathPattern) { this.staticPathPattern = staticPathPattern; }
@ConfigurationProperties( prefix = "spring.mvc" ) public class WebMvcProperties {}
egg3
#spring.activemq.close-timeout spring.activemq.close-timeout=13
public void setCloseTimeout(Duration closeTimeout) { this.closeTimeout = closeTimeout; }
@ConfigurationProperties( prefix = "spring.activemq" )
egg4
server.jetty.accesslog.file-date-format=
public void setFileDateFormat(String fileDateFormat) { this.fileDateFormat = fileDateFormat; }
@ConfigurationProperties( prefix = "server", ignoreUnknownFields = true ) public class ServerProperties {}
egg5
spring.mvc.dispatch-options-request=true
public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) { this.dispatchOptionsRequest = dispatchOptionsRequest; }
@ConfigurationProperties( prefix = "spring.mvc" ) public class WebMvcProperties {}
经历了上面的五个案例观察:上面五个案例都有@ConfigurationProperties这个注解以下面的案例为例:
spring.mvc.dispatch-options-request=false //我在application.properties文件写下的内容 当我点击spring.mvc.dispatch-options-request 是会跳到下面的方法中
//spring.mvc.dispatch-options-request=false public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) { this.dispatchOptionsRequest = dispatchOptionsRequest; } //spring.mvc.dispatch-options-request=true public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) { this.dispatchOptionsRequest = dispatchOptionsRequest; }
你会发现上面的方法在下面的xxxProperties中找到 这个类中有的属性在application.properties文件中都可以写
然后会在下面的类中
@ConfigurationProperties( prefix = "spring.mvc" ) public class WebMvcProperties { private org.springframework.validation.DefaultMessageCodesResolver.Format messageCodesResolverFormat; private final WebMvcProperties.Format format = new WebMvcProperties.Format(); private boolean dispatchTraceRequest = false; private boolean dispatchOptionsRequest = true; private boolean ignoreDefaultModelOnRedirect = true; private boolean publishRequestHandledEvents = true; private boolean throwExceptionIfNoHandlerFound = false; private boolean logRequestDetails; private boolean logResolvedException = false; private String staticPathPattern = "/**"; private final WebMvcProperties.Async async = new WebMvcProperties.Async(); private final WebMvcProperties.Servlet servlet = new WebMvcProperties.Servlet(); private final WebMvcProperties.View view = new WebMvcProperties.View(); private final WebMvcProperties.Contentnegotiation contentnegotiation = new WebMvcProperties.Contentnegotiation(); private final WebMvcProperties.Pathmatch pathmatch = new WebMvcProperties.Pathmatch();
重点在下面的图中
这两个文件有联系
结论:
这就是自动装配的原理精髓所在:
SpringBoot启动会加载大量的自动配置类
我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
我们再来看这个自动配置类中到底配置了哪些组件; (只要我们要用的组件存在在其中, 我们就不需要再手动配置了)
给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;
xxxxAutoConfigurartion:自动配置类;给容器中添加组件xxxxProperties:封装配置文件中相关属性;
Springboot自动配置原理总结:经历了四篇博文的讲述:初步原理到此结束了:我们要知道的内容有以下内容
第一部分:注解的作用
@Target({ElementType.TYPE})ElementType 提供了一个简单的分类:注释可能出现在Java程序中的语法位置(这些常量与元注释类型(@Target)一起指定在何处写入注释的合法位置)
@Retention(RetentionPolicy.RUNTIME)RetentionPolicy,与元注释(@Retention)一起指定注释要保留多长时间
@Documented Documented注解表明这个注释是由 javadoc记录的,在默认情况下也有类似的记录工具。 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分。
@Inherited @inherite 源码
然后,我们打开 inherited 的源码,可以看到:
(1)该注解作用于整个程序运行中(@Retention(RetentionPolicy.RUNTIME);
(2)该注解只能修饰注解(@Target({ElementType.ANNOTATION_TYPE})),因此,它是一个元注解。
@SpringBootApplication:组合注解,包括以下注解。
@SpringBootConfiguration 配置注解,底层其实也是@Configuration注解,只不过在SpringBoot工程中更推荐使用@SpringBootConfiguration来替代
@EnableAutoConfiguration 启动自动配置,根据所依赖的Jar包自动配置相关配置项。(后续会继续解释)
@ComponentScan 扫描配置,SpringBoot默认会扫描启动类所在的同级包及其子包。
第二部分:SpringBoot自动配置原理流程
@SpringBootApplication
@EnableAutoConfiguration
@Import({AutoConfigurationImportSelector.class})
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {}
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
public final class SpringFactoriesLoader {
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";}
Enumeration urls = classLoader.getResources("META-INF/spring.factories");
第三部分:在次深入源码在配置文件中得出的结论:
这就是自动装配的原理精髓所在:
SpringBoot启动会加载大量的自动配置类
我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
我们再来看这个自动配置类中到底配置了哪些组件; (只要我们要用的组件存在在其中, 我们就不需要再手动配置了)
给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;
xxxxAutoConfigurartion:自动配置类;给容器中添加组件xxxxProperties:封装配置文件中相关属性;