SpringBoot配置文件

文件类型

配置文件的绑定

  • 导入配置文件处理器
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  • @ConfigurationProperties(prefix = “person”)
  • @PropertySource(value = “classpath:deng.properties”)
    • 不按application.xxx命名,使用@PropertySource加载指定的配置文件

1、properties文件

  • 语法:key = value
preson.name = 张三
person.age = 18

2、 yaml文件

2.1基本语法

  • 语法:key: value(注意冒号后面要加空格)
  • 使用缩进表示层级关系,因此要格外注意缩进
  • 缩进的空格数不重要,只要层级相同的元素左对齐即可
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private int age;
    private boolean happy;
    private String[] hobbies;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

对应的yaml配置文件写法

person:
#还可以随机生成一些数
  name: 罗${random.uuid}
  age: ${random.int}
  #sex:男	错误写法,冒号后面没有加空格	
  happy: true
  hobbies:[电影,音乐]
  birth: 2021/04/11
  maps: {k1: v1,k2: v2}	#行内写法
  lists:
    - code
    - music
    - girl
  dog:
    name: ${person.hello:hello}_旺财  #前面的值存在使用前面的值,否则使用后面的值
    age: 3
  • 注意事项:value的值不需要使用引号,使用引号表示是否要将转义字符进行转义
    • 单引号:将转义字符当作普通字符,转义字符不进行转义
    • 双引号:转义字符进行转义
person:
	name: '张三 \n 李四'	
	hobby: "电影 \n 音乐"

#单引号转义字符不进行转义	
#name:
#张三 \n 李四
#
#双引号:转义字符进行转义,\n变成换行符
#hobby:
#电影
#音乐

2.2关于yaml扩展

松散绑定
  • 松散绑定:yaml中使用xxx-xxx命名的可以绑定类中的以驼峰命名的属性,如
@Component
@Validated  //数据校验
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
}
person:
	last-name: 阿狸  #last-name的值可以绑定到Person类的lastName属性
JSR303校验

(作用:用于对Java Bean中的字段的值进行验证)
- 在pom.xml中添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
  • 在要进行JSR303校验的类上添加注解@Validated
  • 在要进行校验的属性上添加注解
    @Component
    @Validated  //数据校验
    @ConfigurationProperties(prefix = "person")
    public class Person {
        @NotNull	//name不能为空
        private String name;
        private int age;
        private boolean happy;
        private Date birth;
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }
  • 常用JSR303校验注解
@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;

空检查
@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 对象是否符合正则表达式的规则

.......等等
除此以外,我们还可以自定义一些数据校验规则
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值