1. Verify JSR303 data

  • How to use it?
  • It can be used in Springboot@validatedTo verify the data, if the data is abnormal, the unified exception will be thrown to facilitate the unified processing of the exception center. Let’s make a note here to let’snameOnly Email format is supported.
  • Import dependence
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Copy the code

Person.java

@Component / / registered bean
@ConfigurationProperties(prefix = "person")
@Validated  // Data verification
public class Person {

    @email (message=" mailbox format error ") // Name must be in mailbox format
    private String name;
}
Copy the code
  • Result: Default message [not a valid email address];

2. Use data verification to ensure data correctness;

  • Common parameters
@notnull (message=" name cannot be empty ")
private String userName;
@max (value=120,message=" Max ")
private int age;
@email (message=" mailbox format error ")
privateString email; Empty check@NullVerifies that the object isnull
@NotNullVerifies that the object is notnull, the length cannot be checked0The string@NotBlankCheck whether the constraint string is Null and whether the trimmed length is greater than or equal to0, only for strings, and before and after whitespace is removed.@NotEmptyCheck whether the constraint element is NULL or empty. Booelan check@AssertTrueVerify that the Boolean object istrue  
@AssertFalseVerify that the Boolean object isfalseThe length of the check@Size(min=, max=)Verification objects (Array, Collection, Map, String) length is within a given range@Length(min=, max=)string is between min and max included. Date of inspection@PastVerifies that Date and Calendar objects precede the current time@FutureVerifies that Date and Calendar objects are after the current time@PatternVerify that a String complies with regular expression rules....... In addition, we can also customize some data validation rulesCopy the code

3. Switch between multiple environments

  • profileSpring supports different configuration functions for different environments. You can switch environments quickly by activating different environment versions.

Multiple profiles

  • When we write the main configuration file, the file name can beapplication-{profile}.properties/yml, used to specify multiple environment versions;
  • Springboot will scan the following locationsapplication.propertiesorapplication.ymlFile as the default configuration file for Spring Boot

Such as:

  • Application-test. properties represents the test environment configuration
  • Application-dev.properties represents the development environment configuration

But Springboot does not launch these configuration files directly; it defaults to using the application.properties main configuration file;

We need a configuration to select the environment to activate:

For example, if the dev environment is specified in the configuration file, we can test it by setting a different port number;
If we start SpringBoot, we can see that the configuration has been switched to dev.
spring.profiles.active=dev
Copy the code

Yaml’s multidocument block

  • andpropertiesSame as in configuration file, but usedymlTo implement no need to create multiple configuration files, more convenient!

server:
  port: 8081
Select which environment block to activate
spring:
  profiles:
    active: dev

---
server:
  port: 8082
spring:
  profiles: test The name of the configuration environment

---
server:
  port: 8083
spring:
  profiles: dev The name of the configuration environment

---

server:
  port: 8084
spring:
  profiles: prod  The name of the configuration environment
Copy the code
  • Note: If both YML and Properties are configured with ports and no other environment is activated, they will be used by defaultpropertiesConfiguration file!

Configuration file loading location

  • There are many ways to load configuration files externally, we choose the most common can, in the development of the resource file configuration!
  • Official external configuration file description Reference document

  • Springboot will scan the following locationsapplication.propertiesorapplication.ymlFile as the default configuration file for Spring Boot:
priority1: Indicates the priority of the configuration file in the config folder in the project path2: Indicates the priority of the configuration file in the project path3: Indicates the priority of the configuration file in the config folder in the resource path4: Configuration file in the resource pathCopy the code
  • The configuration with a higher priority overwrites the configuration with a lower priority.

  • SpringBoot loads the master configuration file from all four locations; Complementary configuration;
Server.servlet. context-path=/xdr630Copy the code
  • Specify a location to load the configuration file
  • We can still get throughspring.config.locationTo change the default configuration file location
  • Once the project is packaged, we can specify the new location of the configuration file when starting the project using command-line arguments. In this case, the external configuration file has the highest priority for the same configuration
java -jar spring-boot-config.jar --spring.config.location=F:/application.properties
Copy the code