properties, yml configuration file mapping object

1, properties mapped to the class object file contents (attributes), such as 1.properties Resource files in a directory configured prefix information related to com.imooc.people, then:
Add pom-dependent: springboot -configuration- Processor
People add comments to the class object above:
@Configuration
@PropertySource (value = "CLASSPATH: 1.properties")       // specifies which properties from the content read 
@ConfigurationProperties (prefix = "com.imooc.people")     // prefix specified read 
public  class {
   Private String name;
   Private String age;
。。。name、age的getter/setter。。。
}
Then directly available to the People @Autowired object has attribute values ​​directly in the controller

2, yml mapped to the class object file contents (attributes), YML format does not support @PropertySource annotations introduced, usually with @Value. As 1.properties file in the Resource directory is configured prefix information related to com.imooc.people, then:
@Configuration       // or @Component 
public  class {
  @Value("${com.imooc.people.name}")
  private String name;
  @Value ( "{$ com.imooc.people.age: {null}} #")     // when age fail to correspond to the configuration values, the default value assigned null 
  Private String age;
。。。name、age的getter/setter。。。
}

Guess you like

Origin www.cnblogs.com/afei1759/p/12099449.html