@SpringBootApplication注解SpringBoot深度理解(课时八)

简介: @SpringBootApplication注解SpringBoot深度理解(课时八)

@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:封装配置文件中相关属性;

Spring.Boot Web开发初始静态资源处理《课时九》_星辰镜的博客-CSDN博客

相关文章
|
3天前
|
Java API 数据安全/隐私保护
掌握Spring Boot中的@Validated注解
【4月更文挑战第23天】在 Spring Boot 开发中,@Validated 注解是用于开启和利用 Spring 的验证框架的一种方式,特别是在处理控制层的输入验证时。本篇技术博客将详细介绍 @Validated 注解的概念和使用方法,并通过实际的应用示例来展示如何在项目中实现有效的数据验证
26 3
|
3天前
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
98 1
|
3天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
30 4
|
3天前
|
Java 开发者 Spring
深入理解 Spring Boot 中的 @EnableAutoConfiguration 注解:概念与实践
【4月更文挑战第21天】在 Spring Boot 项目中,@EnableAutoConfiguration 注解是实现自动配置的核心,它可以根据项目的依赖和配置,自动地配置 Spring 应用程序中的 Bean
35 3
|
3天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
3天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
1天前
|
Java
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
|
3天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
3 0
|
3天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
7 0
|
3天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解