目录
SpringBoot简介
- 创建独立的Spring应用程序
- 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
- 提供自以为是的“入门”依赖项,以简化构建配置
- 尽可能自动配置Spring和3rd Party库
- 提供生产就绪的功能,例如指标,运行状况检查和外部配置
- 完全没有代码生成,也不需要XML配置
Hello world
- idea创建Springboot项目
项目目录:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String helloWorld(){
return "Hello SpringBoot";
}
}
运行SpringbootProjectApplication
SpringBoot注解分析
- @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) })
public @interface SpringBootApplication {
- @SpringBootConfiguration:SpringBoot配置类
- @EnableAutoConfiguration:开启自动配置功能; 开启自动配置功能才能使配置生效
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
- @AutoConfigurationPackage:自动配置包
-
@Import(AutoConfigurationPackages.Registrar.class):
Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class;
将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
AutoConfigurationImportSelector:自动导入哪些选择器组件,将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
- 导入配置文件中的自动配置类,路径:META-INF/spring.factories
SpringBoot配置
- SpringBoot的配置文件有两种一种是YML(YAML),另一种是proproperties,properties是经常使用的一些配置文件,这里的配置主要介绍YML(YAML)的配置形式。
- YML(YAML)以数据为中心
- YML语法
- k:(空格)v:表示一对键值对(空格必须有);
- 属性和值也是大小写敏感;
- 字符串默认不用加上单引号或者双引号;
- 里面可以写Map list 布尔值 对象
server:
port: 8081
person:
name: 张三
age: 18
birth-day: 2020/01/01
boss: true
map: {a: aa,b: bb,c: cc}
list:
- 1
- 2
- 3
cat:
name: xiaomao
age: 2
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private Date birthDay;
private boolean boss;
private Map<String,Object> map;
private List<String> list;
private Cat cat;
set.....
get.....
toString....
}
-
导入配置处理器,绑定了类后配置文件就会有提示
<!--导入配置处理器,绑定了类后配置文件就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor </artifactId>
<optional>true</optional>
</dependency>
-
@Value获取值和@ConfigurationProperties获取值比较
注解 @ConfigurationProperties @Value 功能 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法) 支持 不支持 SpEL 不支持 支持 JSR303数据校验 支持 不支持 复杂类型封装 支持 不支持
@ConfigurationProperties如果使用javabean和配置文件进行映射就可以使用这个注解
- 在配置文件中可以使用占位符获取前面数据指定的值,没有的话可以给一个默认值
cat:
name: ${person.name}.xiaomao
age: 2
cat:
name: ${person.name1:李四}.xiaomao
age: 2
- 可以获取随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
-
Profile配置
Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活指定参数等方式快速切换环境
多Profile文件时:我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml默认使用application.properties的 配置;
- yml支持多文档块方式
spring:
profiles:
active: pro
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: pro
---
server:
port: 8083
spring:
profiles: test
- 激活指定的profile
1、在配置文件中指定 spring.profiles.active=dev
2、命令行:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
可以直接在测试的时候,配置传入命令行参数
3、虚拟机参数;
-Dspring.profiles.active=dev
-
配置文件加载位置
springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件
–file:./config/
–file:./
–classpath:/config/
–classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置全部加载主配置文件;互补配置;