SpringBoot学习---自动装配原理、主启动类运行机制、yaml、JSR303校验

    科技2022-08-02  104

    修改项目端口号

    在application.properties配置文件中输入server.post=8081 重启项目即可修改端口号。

    原理初探

    在pom.xml中

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>

    点击进入spring-boot-starter-parent底层,ctrl+鼠标左键

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.4.RELEASE</version> </parent>

    spring-boot-dependencies存储核心依赖,在父工程中 在写和引入Springboot的时候,不需要引入版本,在工程已经全部导入

    启动器

    <!--启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>

    启动器:Springboot启动场景 它会将所有的功能场景,全部都变成启动器 如果需要使用什么功能,直接找到启动器即可

    主程序

    package com.ty.study;

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

    //标注这个项目是一个Springboot的项目 @SpringBootApplication public class StudyApplication { //将springboot启动,通过反射 public static void main(String[] args) { SpringApplication.run(StudyApplication.class, args); } }

    点击SpringBootApplication进入底层,查看 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration //核心 @EnableAutoConfiguration //核心 @ComponentScan( //扫描下面的包 配置扫描 excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )}

    拆分理解

    @SpringBootConfiguration //核心

    springboot的配置,点击进入查看底层,

    @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration //代表spring配置类 点击,在查看底层 @Target({ElementType.TYPE}) //原注解 @Retention(RetentionPolicy.RUNTIME) //原注解 @Documented//原注解 @Component 说明这也是一个spring注解

    @EnableAutoConfiguration //核心 自动配置

    记住:metadata元数据 @AutoConfigurationPackage //自动配置包 @Import({Registrar.class})/导入选择器包注册 @Import({AutoConfigurationImportSelector.class}) //自动配置导入选择 if (!this.isEnabled(annotationMetadata)) { //第64行程序 查看annotationMetadata 获得所有配置 List configurations = this.getCandidateConfigurations(annotationMetadata, attributes); 点击getCandidateConfigurations 底层 //获取所有候选配置 protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, “No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.”); return configurations; }

    找到

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."); return configurations; } protected Class<?> getSpringFactoriesLoaderFactoryClass() { return EnableAutoConfiguration.class; }

    return EnableAutoConfiguration.class;

    这里又回到了 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} )

    META-INF/spring.factories. 自动配置核心文件 点击spring.factories查看 Initializers 初始化 Application Listeners 监听 Auto Configuration Import Listeners 监听 Auto Configuration Import Filters 自动选择导入的包 Auto Configure 自动配置 Failure analyzers Template availability providers

    回到这里的方法AutoConfigurationImportSelector.class,点击loadFactoryNames,(119行) Properties properties = PropertiesLoaderUtils.loadProperties(resource); 所有资源加载到配置类中

    回到WebMvcAutoConfiguration.java类, @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) @AutoConfigureOrder(-2147483638) @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})

    @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) 条件判断是否注入

    思维导图地址:https://www.processon.com/mindmap/5f7a82c3e401fd06fd778dd7

    Springboot都是的自动配置都在启动的时候扫描并加载;spring.factories,所有自动配置类哦都在这里,不一定生效,判断是否生效,需要导入对应的start就有了对应的启动器,有了启动器,就会自动装配就会生效,然后就配置成功了。

    1.springboot在启动加载时,从类路径中/META-INF/spring.factories获取指定的值 2.将自动配置的类导入容器,自动配置类就会生效,帮我们自动配置 3.以前需要自动配置的,现在都给我们配置了 4.整合javaEE,解决方案和自动装配都在spring-boot-autoconfigure-2.3.4.RELEASE.jar下面 5.会把所有需要导入的组件以类名的方式返回,这些类名就会被添加到容器中 6.容器中也会存xxxAutoConfiguration的文件,给这个容器中导入了这个场景需要的所有组件,并自动配置,@Config,javaConfig, 7.有了自动配置,减去大量工程

    主启动类运行机制

    run方法: StringApplication工作内容: 推断项目类型:普通项目/web项目 查找并初始化所有的初始化器,设置到initializers属性中 找出所有应用程序的监听类,设置到listener属性中 推断并设置main方法的定义类,运行到主类中

    核心:javaConfig @configuration @Bean

    关于SpringBoot 谈谈理解 1.自动装配 2.run()

    yaml

    yaml基础语法 在resource文件目录下面新建一个application.yaml配置文件 在pom.xml中的spring-boot-starter-parent 可以看到支持到处yaml #对空格要求很严格 server: port: 8081

    普通的k: v people: name: ty age: 2

    行内写法 student: {name: ty, age: 2}

    #数组 students:

    nameage

    stu: [name,age]

    属性

    可以给实体类赋值

    yaml注入文件配置

    创建实体类Dog

    @Component public class Dog { private String name; private Integer age; //生成无参、有参、get、set方法、toString }

    创建实体类Person

    @Component //javaConfig 绑定配置文件得方式 @ConfigurationProperties(prefix = “person”) //方式一 主要得1 //加载指定配置文件2 //@PropertySource(value = “classpath:ty.properties”) public class Person { // Person{ // name=‘ty’, // age=20, // happy=true, // birth=Tue Jun 08 00:00:00 CST 1999, // maps={k1=v1, k2=v2}, // lists=[music, code, girl], // dog=Dog{name=‘tt’, age=2}}

    //方式二 SPEL表达式取出取中得值2

    // @Value("$ {name}") private String name; // @Value("$ {age}") private Integer age; // @Value("${happy}") private Boolean happy; private Date birth; private Map<String,Object> maps; private List lists; private Dog dog;

    //生成无参、有参、get、set方法、toString

    }

    application.yaml配置文件

    Person: name: ty${random.uuid} #值为uuid age: ${random.int} # 值为int类型得random happy: true birth: 1999/6/8 maps: {k1: v1,k2: v2} hello: xx lists: - music - code - girl dog: name: ${Person.hello:hello}_tt #如果Person.hello存在就输出hello 默认hello_tt age: 2

    ty.properties配置文件

    name=ty age=3 happy=true

    测试文件

    @SpringBootTest class Springboot02ConfigApplicationTests { @Autowired private Person person; @Test void contextLoads() { System.out.println(person); } }

    yanl和properties得区别 @ConfigurationProperties和@Value得区别 区别:@ConfigurationProperties只需要写一次,而@Value需要在每一个字段都需要 选择yaml,即可。

    JSR303校验

    空检查 @Null 验证对象是否为null @NotNull 验证对象是否不为null, 无法查检长度为0的字符串 @NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格. @NotEmpty 检查约束元素是否为NULL或者是EMPTY.

    Booelan检查 @AssertTrue 验证 Boolean 对象是否为 true @AssertFalse 验证 Boolean 对象是否为 false

    长度检查 @Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内 @Length(min=, max=) string is between min and max included.

    日期检查 @Past 验证 Date 和 Calendar 对象是否在当前时间之前 @Future 验证 Date 和 Calendar 对象是否在当前时间之后 @Pattern 验证 String 对象是否符合正则表达式的规则

    …等等 除此以外,我们还可以自定义一些数据校验规则

    参考学习视频:https://www.bilibili.com/video/BV1PE411i7CV?p=13 参考学习博客:https://blog.csdn.net/DDDDeng_/article/details/107327942?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-11-107327942.nonecase&utm_term=spring 狂神说&spm=1000.2123.3001.4430

    Processed: 0.014, SQL: 9