qyx66

Spring Boot

author:QYX

前言:因为原本是md文件代码就不转了,嫌麻烦,很早的时候写的,版本可能有点老了,更新了2.0的一些特性

一、hello world实现与底层解析

@SpringBootApplication: spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就用这个类的main方法来启动Springboot应用

 
 @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的配置类

标注在某个类上,表示这是一个springboot的配置类

@Configuration:配置类上来标注这个注解

配置类-----配置文件 配置类也是容器中的一个组件 @Component

@EnableAutoConfiguration:开启自动配置功能

以前我们需要配置的东西,SpringBoot帮我们配置,该注解告诉spring boot开启配置功能,这样自动配置才能生效

 @AutoConfigurationPackage
 @Import({AutoConfigurationImportSelector.class})
 public @interface EnableAutoConfiguration {

@AutoConfigurationPackage:自动配置包

 @Import({Registrar.class})

Spring 的底层注解,给容器中导入一个组件,导入的组件由Registrar.class规定

将主配置类的(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器

@Import({AutoConfigurationImportSelector.class}):导入哪些组件的选择器

AutoConfigurationImportSelector:将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中,会给容器中导入非常多的自动配置类,

(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件,

有了这些自动配置类,就免去了我们手动编写配置注入功能组件等工作。

 SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration,classloader)

从类路径下的META-INF/spring-factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入容器中,自动配置类就会生效,帮我进行自动配置工作。

J2EE的整体整合方案和自动配置都在是spring-boot-autoconfigure-1.5.9.RELEASE.jar

今天使用Spring boot 的Test方法遇到了以下错误

二、yml与properties语法及使用

一、yml语言实战

代码模块

bean类

 package com.example.demo.bean;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.DoubleAccumulator;
 /**
  * 将配置文件中的配置每一个属性的值,映射到这个组件中
  * @ConfigurationProperties,告诉spring boot将本类中的所有属性与配置文件中的相关配置进行绑定
  * prefix = "person"配置文件中哪个下面的所有属性进行一一映射
  * 只有这个组件是容器中的组件,才能使用容器提供的功能
  */
 @Component
 @ConfigurationProperties(prefix = "person")
 public class Person
 {
     @Override
     public String toString() {
         return "Person{" +
                 "lastName=\'" + lastName + \'\\'\' +
                 ", age=" + age +
                 ", boss=" + boss +
                 ", birth=" + birth +
                 ", maps=" + maps +
                 ", lists=" + lists +
                 ", dog=" + dog +
                 \'}\';
    }
 
     private String lastName;
     private Integer age;
     private boolean boss;
     private Date birth;
     private Map<String,Object> maps;
     private List<Object> lists;
     private Dog dog;
 
     public String getLastName() {
         return lastName;
    }
 
     public void setLastName(String lastName) {
         this.lastName = lastName;
    }
 
     public Integer getAge() {
         return age;
    }
 
     public void setAge(Integer age) {
         this.age = age;
    }
 
     public boolean isBoss() {
         return boss;
    }
 
     public void setBoss(boolean boss) {
         this.boss = boss;
    }
 
     public Date getBirth() {
         return birth;
    }
 
     public void setBirth(Date birth) {
         this.birth = birth;
    }

分类:

技术点:

相关文章:

  • 2021-12-28
  • 2021-12-03
  • 2021-05-01
  • 2020-06-09
  • 2021-10-31
  • 2021-08-13
  • 2021-05-29
猜你喜欢
  • 2022-02-09
  • 2021-10-31
  • 2019-08-18
  • 2021-12-05
  • 2021-06-12
相关资源
相似解决方案