Configuration file attributes, and how to map entity class
- Use spring-boot-configuration-processor reads the configuration file more gracefully
Simple configuration file read
@Value("${author.name}") private String authorName;
使用spring-boot-configuration-processor
The first step: the introduction of pom file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Step two: Defining the configuration file, the file directory on the resources, the file format: .properties file name (eg: author.properties), file contents as follows:
author.name = your name author.email = your email address
The third step: class definition configuration, add annotations
@Configuration @ConfigurationProperties(prefix = "author") @PropertySource(value = "classpath:author.properties") public class AuthorConfig { private String name; private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Step Four: Use
@RestController public class HelloController { @Autowired private AuthorConfig authorConfig; @Value("${author.name}") private String AuthorName; @GetMapping("/hello") public void Hello(){ System.out.println(AuthorName); System.out.println(authorConfig.getName()); System.out.println(authorConfig.getEmail()); } }