@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(); } }