资讯详情

yml-jrs303-springboot测试

yaml

springboot使用全局配置文件(名称固定):

  • application.properties

  • application.yml

优先级

注意:如果yml和properties端口同时配置,没有激活其他环境 , 默认会使用properties配置文件!

优先级1:项目路径下config文件夹配置文件 优先级2:在项目路径下配置文件 优先级3:资源路径下config文件夹配置文件 优先级4:资源路径下配置文件

静态资源 我们可以识别存储在四个目录中的静态资源

"classpath:/META-INF/resources/" "classpath:/resources/" "classpath:/static/" "classpath:/public/"

定制静态资源路径

YAML语法

k: v:表示键值对(必须有空格)。 只要左对齐的数据是同一级别的,就用空间缩进来控制层次关系。

server:   port: 8081

@Value 与 @ConfigurationProperties 对比

@Value 和 @ConfigurationProperties 注释可以读取配置文件中的属性值并绑定到 JavaBean 但两者有以下区别。

  1. 不同的使用位置

  • @ConfigurationProperties:标注在 JavaBean 的类名上;

  • @Value:标注在 JavaBean 的属性上。

  1. 功能不同

  • @ConfigurationProperties:批量绑定配置文件中的配置;

  • @Value:需要绑定的配置只能一一指定。

使用 注解

@Component @ConfigurationProperties(prefix = "person")

使用 注解

@Value("${sex}") String sex; @Value("${msg1}") String msg1; @Value("${msg2}") String msg2; @Value("${person.name}") String name; @Value("${person.age}") int age; @Value("${address[0]}") String address1; @Value("${address[1]}") String address2;

注解

阅读配置内容

@value

读取单个值

Environment

@ConfigurationProperties(prefix = "person")

读取yml指定对象的值

yml

 #测试yml读取对象 user:   id: 123   name: "hhh"   password: "qwas258"   age: "18"   sex: "男" 

bean

package com.wjx.demo_12_16.bean;   import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;  @Data @Component @ConfigurationProperties(prefix = "user") public class User {   private int id;   private String name;   private String password;   private int age;   private String sex; } 

test

package com.wjx.demo_12_16;  import com.wjx.demo_12_16.bean.User; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;  import javax.annotation.Resource;  @SpringBootTest class ApplicationTests {    @Resource   User user;    @Test   void contextLoads() {     System.out.println(user);    }  } 

结果

User(id=123, name=Lenovo, password=qwas258, age=18, sex=男)

JSR303

可在后台继续数据检验

<!-- JSR303 -->         <dependency>             <groupId>javax.validation</groupId>             <artifactId>validation-api</artifactId>         </dependency>

类型

@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 对象是否符合正则表达式规则
@Data @NoArgsConstructor @AllArgsConstructor @Component @ConfigurationProperties(prefix = "person") @Validated///数据校验 public class Person {     @NotNull     private String name;     private Integer age;     private Boolean happy;     private Date birth;     private Map<String,Object> map;     private Dog dog;     @Email     private String email; }

package com.wjx.demo_12_16.bean;   import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated;  import javax.validation.constraints.Email;  @Data @Component //装配bean @ConfigurationProperties(prefix = "user") //配置yml ///数据检查 @Validated public class User {     @Email(message = "邮箱格式错误")     private int id;     private String name;     private String password;     private int age;     private String sex; } 

标签: jrs1ds过载继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台