spring-boot-configuration-processor

spring yml default configuration, but sometimes use traditional or xml configuration properties, you need to use spring-boot-configuration-processor up

The introduction of pom-dependent

 <dependency>
     <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>

 

author.name=zhangsan
author.age=20

And then at the beginning of class configuration plus @PropertySource ( "classpath: your.properties"), the remaining usage and load configuration as yml

@Component
@PropertySource(value = {"classpath:static/config/authorSetting.properties"},
        ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties") public class AuthorTest { @Value("${author.name}") private String name; @Value("${author.age}") private int age; } 

@PropertySource attributes explained 
1.value: specify the path configuration file loading. 
2.ignoreResourceNotFound: the specified configuration file does not exist whether or not an error, the default is false. When set to true, if the file does not exist, the program does not complain. The actual project development, it is best to set ignoreResourceNotFound to false. 
3.encoding: encoding properties file used to specify read, we usually use the UTF-8.

When we use @Value need to inject more value, the code will become redundant, so @ConfigurationProperties debut

@Component
@ConfigurationProperties(prefix = "author")
@PropertySource(value = {"classpath:static/config/authorSetting.properties"}, ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties") public class AuthorTest { private String name; private int age; } 
@RestController
@EnableConfigurationProperties
public class DemoController { @Autowired AuthorTest authorTest; @RequestMapping("/") public String index(){ return "author's name is " + authorTest.getName() + ",ahtuor's age is " + authorTest.getAge(); } } 

Use @EnableConfigurationProperties open @ConfigurationProperties comment.

Guess you like

Origin www.cnblogs.com/xww115/p/11412560.html