springboot基础介绍
1.配置文件的使用
全局配置文件 SpringBoot使用一个全局的配置文件,配置文件名固定,作用是修改SpringBoot的默认配置。
application.properties(默认加载)
application.yml
YAML 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写。 在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。 YAML的语法和其他高级语言类似,并且可以简单表达清单、散列表,标量等数据形态。 它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、文件大纲(例如:许多 电子邮件标题格式和YAML非常接近)。 YAML 的配置文件后缀为 .yml,如:runoob.yml 。
1.2.2、yml 1、基本语法
key: value
key1: key1-1: value key1-2: value
......
2、yml描述数据
(1)字面量:数字、字符串、布尔
age: 28
字符串不用添加引号
name: tom name: "tom \n jack" tom jack name: 'tom \n jack' tom \n jack
boolean: true
(2)对象/Map
普通写法
student: name: tom age: 18
行内写法
student: {name: tom, age: 18}
1.2.3、获取配置文件中的值 (1)@ConfigurationProperties、@Value
配置文件:
application.yml
对象
student: # 字面量 name: tom age: 17 birthday: 2019/01/01 married: false
集合
list: [1,2,3,4,5] map: {k1: v1, k2: v2}
对象
dog: name: 豆豆 age: 1
@ConfigurationProperties
/**
- @ConfigurationProperties(prefix = "student")
- 本类中所有的属性和配置文件中的属性绑定,prefix是配置文件中标识该对象的前缀 *
- @Component
- 只有在spring容器中的组件才能使用@ConfigurationProperties功能,所以要添加该注解 *
- @Validated
- 数据校验 */ @Component @ConfigurationProperties(prefix = "student") @Validated public class Student { @Email // name的值必须是邮件类型 private String name; private Integer age; private Date birthday; private Boolean married; private List list; private Map<String, Object> map; private Dog dog; // 省略set/get }
校验导包:
org.hibernate hibernate-validator 6.1.5.Final
自定义类在yml中的提示
org.springframework.boot spring-boot-configuration-processor
@Value
@Component public class Student { @Value("${student.name}") private String name; @Value("123") private Integer age; @Value("2019/01/01") private Date birthday; @Value("true") private Boolean married; @Value("9,8,7,6") private List list; private Map<String, Object> map; private Dog dog; // 省略set/get }
17 @ConfigurationProperties和@Value区别:
@ConfigurationProperties @Value 功能 批量注入 需要在每一个属性上配置 松散绑定 支持 不支持 SpEL 不支持 支持 JSR303数据校验 支持 不支持 复杂类型封装 支持 不支持 松散绑定值得是给属性赋值时的写法:
写法一:标准驼峰命名 -> person.firstName 写法二:大写使用下划线表示 -> person.first_name 写法三:大写使用横杠表示 -> person.first-name
1 2 3 4 (2)@PropertySource、@ImportResource
@PropertySource:加载指定配置文件
@PropertySource(value = {"classpath:student.properties"}) @Component @ConfigurationProperties(prefix = "student") public class Student { @Value("${person.name}") private String name; @Value("123") private Integer age; @Value("2019/01/01") private Date birthday; @Value("true") private Boolean married; @Value("9,8,7,6") private List list; private Map<String, Object> map; private Dog dog;
}
@ImportResource: 导入spring的配置文件,让配置文件中的内容生效; SpringBoot中没有Spring的配置文件,自己编写的配置文件放到资源路径下不能生效。想让Spring配置 文件生效,需要在一个配置类(比如:主程序类)上添加@ImportResource(location={})注解把Spring的 配置文件加载进来。
SpringBoot中给容器中添加组件的推荐方式:
编写一个配置类(把原来的xml使用Java代码描述),使用@Bean给容器中添加组件
/**
-
@Configuration:标识该类是一个配置类,用于替换之前的xml文件 / @Configuration public class MyConfig{ /
- 将方法的返回值添加到容器中
- 容器中这个组件的id默认是方法名 *
- @Bean 用于替换xml中的bean标签 */ @Bean public Student student(){ return new Student(); } }
1.2.4、配置文件占位符 1、随机
{random.value}、${random.uuid}
2、表达式获取配置的值
student.name=Peter_{random.uuid} student.age=18 student.birthday=2019/01/01 student.married=false
student.list=1,2,3,5,6 student.map.k1=v1 student.map.k2=v2
student.dog.name=${student.name:小狗} student.dog.age=2
-