Preface: This article is compiled by the editor of Programming Notes#, mainly introduces the knowledge related to springboot custom configuration, and hopes to have certain reference value for you.
springboot custom configuration (to solve automatic prompt Chinese garbled characters)
1. Description
In the development of springboot, we sometimes need to write some parameters into the yml configuration for easy modification after deployment. At this time, we can use the custom configuration function provided by springboot
2. Introduce dependencies
dependency>
groupId>org.springframework.boot</groupId>
artifactId>spring-boot-configuration-processor</ artifactId>
optional>true</optional>
</dependency>
3. Write a custom configuration class
Example:
import lombok.Data;
import org.springframework.boot.context. properties.ConfigurationProperties;
/**
* Some configurations of the client
*/
@ConfigurationProperties(prefix = “client.config”)
@Data
public class ClientConfig {
/**
* base url
*/
private String baseUrl;
}
4. Use IDEA to compile and generate spring -configuration-metadata.json file
The spring-configuration-metadata.json file generated after compilation is in the following path:
The role of the spring-configuration-metadata.json file is Let us provide automatic prompts when entering configurations in yml or properties files, as follows:
Also: If the above automatic prompt appears Chinese garbled characters, change spring-configuration-metadata.json The encoding format of the file is changed from utf-8 to gbk, and the Chinese prompt will be displayed normally. The specific operation is as follows:
1) Click the encoding in the lower right corner
2) Switch to gbk encoding
3) Encoding format of covert file
4) Confirm
5) Go to yml and enter the configuration, you can see the automatic prompt in Chinese The display is already normal
5. Inject custom configuration and use
1 ) Add annotations to the startup class
@EnableConfigurationProperties({ClientConfig.class})
2) Use configuration
@Autowired
private ClientConfig config;
.. .//Omit some code here
System.out.println(“customized configuration :”+config.getBaseUrl());