环境问题:
<dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.5.2</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
3.JSR303数据校验需要导入的依赖!
<dependency><!--数据校验依赖,为了实现@Email注解--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
在resources目录下创建一个banner.txt文件
(¸¸.♥➷♥•*¨)¸.•´¸.•*¨) ¸.•*¨) (¸.•´(¸. ¸.•´¸.•*¨) ¸.♥➷•*¨) ─▀██▀─▄███▄─▀██─██▀██▀▀▀█─ ──██─███─███─██─██─██▄█──conquers ──██─▀██▄██▀─▀█▄█▀─██▀█──all ♥➷♥ ─▄██▄▄█▀▀▀─────▀──▄██▄▄▄█ ¸.•´¸.•*¨) ¸♥.•*¨) (¸.•´¸♥➷♥¸.•´♥¸.•´♥¸.•*¨)♥.•*¨)¸.•*♥¸
就可以修改SpringBoot启动时的图案显示
说明:语法要求严格!
实例:
package com.chen.pojo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component/*相当于<Bean id="" ...> 类注入功能*/ @ConfigurationProperties(prefix = "person") public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; public Person() { } public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) { this.name = name; this.age = age; this.happy = happy; this.birth = birth; this.maps = maps; this.lists = lists; this.dog = dog; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", happy=" + happy + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getHappy() { return happy; } public void setHappy(Boolean happy) { this.happy = happy; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }
person: name: cjs${random.uuid} age: ${random.int} happy: false birth: 2021/11/6 maps: {k1: v1,k2: v2} lists: -code -music -girl dog: name: ${person.name:chen}_cjs age: 5
package com.chen; import com.chen.pojo.Dog; import com.chen.pojo.Person; import com.chen.pojo.TestUser; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class Springboot02ConfigApplicationTests { @Autowired/*Spring自动装配对象*/ private Person person; @Test void contextLoads() { System.out.println(person); } }
可以发现,在yml文件的数据可以赋值到对象对应的属性中。
@ConfigurationProperties(prefix = "person")这个注解是绑定类与yml的
prefix的值要对应实例化的参数名!
结论,配置yml和配置properties都可以获取到值,强烈推荐 yml;
如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;
如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties,不要犹豫!
创建一个实体类:
package com.chen.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.*; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor @Component @Validated/*一定要开这个注解,才能进行JSR303的数据校验*/ @ConfigurationProperties(prefix = "user") public class TestUser { @NotNull(message = "名字不能为空") private String username; @Max(value = 120,message = "你的岁数真大啊") private int age; @Email(message = "邮箱格式错误") private String email; @Past(message = "生日输入有误") private Date birth; }
创建对应的yml文件:
user: username: cjs age: 100 email: 1983691635@qq.com birth: 2000/12/25
测试类:
package com.chen; import com.chen.pojo.Dog; import com.chen.pojo.Person; import com.chen.pojo.TestUser; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class Springboot02ConfigApplicationTests { @Autowired/*Spring自动装配对象*/ private TestUser user; @Test void contextLoads() { System.out.println(user); } }
可以看到,利用JSR303的数据校验来对实体类中的数据进行多种的检验。
注意:一定要在实体类上加上@Validated注解,才能开启数据校验。
profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境。
创建yaml文件:
server: port: 8082 #选择要激活那个环境块 spring: profiles: active: dev --- server: port: 8083 spring: profiles: dev --- server: port: 8084 spring: profiles: test ---
运行SpringBoot主程序,发现端口号改变了。
结论,可以通过修改active中的值,来修改SpringBoot的端口号,
同理,在开发中我们可以利用这种方式来修改程序环境!
优先级1:项目路径下的config文件夹配置文件
优先级2:项目路径下配置文件
优先级3:资源路径下的config文件夹配置文件
优先级4:资源路径下配置文件