springboot(3)-loose binding, JSR303 data verification, multi-environment switching

1 Loosely bound

The last-name written in yml is the same as lastName.-The letters following are capitalized by default, which is loose binding.

Dog.java

package com.zs.helloword.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
    
    
    private String lastName;
    private Integer age;

    public Dog() {
    
    
    }

    public Dog(String lastName, Integer age) {
    
    
        this.lastName = lastName;
        this.age = age;
    }

    public String getLastName() {
    
    
        return lastName;
    }

    public void setLastName(String lastName) {
    
    
        this.lastName = lastName;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Dog{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                '}';
    }
}

HellowordApplicationTests.java

package com.zs.helloword;

import com.zs.helloword.pojo.Dog;
import com.zs.helloword.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

// 单元测试
@SpringBootTest
class HellowordApplicationTests {
    
    
    @Autowired
    private Dog dog;
    @Test
    void contextLoads() {
    
    
        System.out.println(dog);
    }

}

application.yaml

dog:
  last-name: 旺财

print

Dog{
    
    lastName='旺财', age=null}

2 JSR303 data verification

@Validated can be used to verify data in Springboot .
program

Person.java

package com.zs.helloword.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;


@Component
@ConfigurationProperties(prefix = "person")
@Validated // 数据校验
public class Person {
    
    

    @Email(message="邮箱格式错误") //name必须是邮箱格式
    private String name;

    public Person() {
    
    
    }

    public Person(String name) {
    
    
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

HellowordApplicationTests.java

package com.zs.helloword;

import com.zs.helloword.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

// 单元测试
@SpringBootTest
class HellowordApplicationTests {
    
    
    @Autowired
    private Person person;
    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

application.yaml

person:
  name:99

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zs</groupId>
    <artifactId>helloword</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloword</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- web场景启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.2</version>
            </plugin>

        </plugins>
    </build>

</project>

Print
Insert picture description here
common parameters

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

.......等等
除此以外,我们还可以自定义一些数据校验规则

3 Multi-environment switching

1 Multiple configuration files When
we write the main configuration file, the file name can be application-{profile}.properties/yml, which is used to specify multiple environment versions;
for example:
application-test.properties represents the test environment configuration
application-dev. Properties represents the development environment configuration,
but Springboot does not directly start these configuration files. It uses the application.properties
configuration file
by default : we need to select the environment that needs to be activated through a configuration:

#比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试;
#我们启动SpringBoot,就可以看到已经切换到dev下的配置了;
spring.profiles.active=dev

2 The multi-document block of yaml
is the same as in the properties configuration file, but using yaml to implement it does not need to create multiple configuration files.
Program
application.yaml

server:
  port: 8080
# 选择要激活的那个环境块
spring:
  profiles:
    active: dev

---
server:
  port: 8081
spring:
  profiles: dev

Run HellowordApplication to
print

Tomcat initialized with port(s): 8081 (http)

3 The loading location of the configuration file.
Insert picture description here
Among them:
file path refers to the project path and
classpath refers to the class path, namely main/java or resources path

Order of priority

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

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/112424263