@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博客

目录
打赏
0
0
0
0
6
分享
相关文章
|
6天前
|
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
34 16
|
16天前
|
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
103 26
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
169 89
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
61 21
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
181 73
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
【Spring配置】idea编码格式导致注解汉字无法保存
问题一:对于同一个项目,我们在使用idea的过程中,使用汉字注解完后,再打开该项目,汉字变成乱码问题二:本来a项目中,汉字注解调试好了,没有乱码了,但是创建出来的新的项目,写的注解又成乱码了。
Spring MVC中的请求映射:@RequestMapping注解深度解析
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的关键,它将HTTP请求映射到相应的处理器方法上。本文将深入探讨`@RequestMapping`注解的工作原理、使用方法以及最佳实践,为开发者提供一份详尽的技术干货。
234 2
探索Spring MVC:@Controller注解的全面解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序的基石之一。它不仅简化了控制器的定义,还提供了一种优雅的方式来处理HTTP请求。本文将全面解析`@Controller`注解,包括其定义、用法、以及在Spring MVC中的作用。
84 2
Spring MVC中的控制器:@Controller注解全解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序控制层的核心。它不仅简化了控制器的定义,还提供了灵活的请求映射和处理机制。本文将深入探讨`@Controller`注解的用法、特点以及在实际开发中的应用。
169 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等