@ConfigurationProperties : 是springboot的註解,用於把主配置文件中配置屬性設置到對於的Bean屬性上java
@PropertySource :是spring的註解,用於加載指定的屬性配置文件到Spring的Environment中,能夠和 @Value、@ConfigurationProperties配合使用spring
@EnableConfigurationProperties : 用來開啓ConfigurationProperties註解配置;若是不使用的話,@ConfigurationProperties加入註解的類上加@Component也是能夠交於springboot管理。springboot
application.yml配置:
app
實現方式一 @ConfigurationProperties + @Component做用於類上ide
@ConfigurationProperties(prefix="person") @Componment @Data // lombok,用於自動生成getter、setter public class Person { private String name; } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }
實現方式二 @ConfigurationProperties + @Bean做用在配置類的bean方法上spa
@Data public class Person { private String name; } @Configuration public class PersonConf{ @Bean @ConfigurationProperties(prefix="person") public Person person(){ return new Person(); } } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }
實現方式三 @ConfigurationProperties註解到普通類、 @EnableConfigurationProperties註解定義爲beancode
@ConfigurationProperties(prefix="person") @Data public class Person { private String name; } // 說明: @EnableConfigurationProperties能夠直接注到啓動類上,也能夠放在自定義配置類,自定義配置類使用@Configuration標註 @SpringBootApplication @EnableConfigurationProperties(Person.class) public class DbApplication { public static void main(String[] args) { SpringApplication.run(DbApplication.class, args); } } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }
實現方式四 @Value做用屬性上對象
@RestController @RequestMapping("/db") public class TestController { @Value("${person.name}") private String name; @GetMapping("/person") public String parsePerson() { return name; } }
實現方式五 使用自帶的Environment對象blog
@RestController @RequestMapping("/db") public class TestController { @Autowired private Environment environment; @GetMapping("/person") public String parsePerson() { return environment.getProperty("person.name"); } }
dangxiaodang.properties配置:(說明: PropertySource不支持yml、yaml,詳細請看擴展內容)
繼承
實現方式一 @Configuration + @PropertySource + Environment
@Data public class Person { private String name; } @Configuration @PropertySource(value = "classpath:dangxiaodang.properties") public class PersonConf { @Autowired private Environment environment; @Bean public Person person(){ Person person = new Person(); person.setName(environment.getProperty("person.name")); return person; } } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }
實現方式二 @Configuration + @PropertySource + @Value
@Component @PropertySource(value = "classpath:dangxiaodang.properties") @Data public class Person { @Value("${person.name}") private String name; } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }
實現方式三 @Configuration + @PropertySource + @ConfigurationProperties
@Component @PropertySource("classpath:dangxiaodang.properties") @ConfigurationProperties(prefix = "person") public class Person{ private String name; } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }
public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(resource.getResource()); factory.afterPropertiesSet(); Properties ymlProperties = factory.getObject(); String propertyName = name != null ? name : resource.getResource().getFilename(); return new PropertiesPropertySource(propertyName, ymlProperties); } } @Component @PropertySource(value = "classpath:dangbo.yml", factory = YamlPropertySourceFactory.class) // 指定對應的factory @ConfigurationProperties(prefix = "person") @Data public class Person { private String name; } @RestController @RequestMapping("/db") public class TestController { @Autowired private Person person; @GetMapping("/person") public String parsePerson() { return person.getName(); } }