在Spring Boot中使用 @ConfigurationProperties 和 @EnableConfigurationProperties
2019-01-08 14:08
603 查看
Spring Boot 使用一些轻松的规则来绑定属性到
@ConfigurationPropertiesbean 并且支持分层结构(hierarchical structure)。
开始创建一个
@ConfigurationPropertiesbean:
[code]@ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") public class Mai 4000 lProperties { private String host; private int port; private String from; private String username; private String password; private Smtp smtp; // ... getters and setters @Override public String toString() { return "MailProperties [host=" + host + ", port=" + port + ", from=" + from + ", username=" + username + ", password=" + password + ", smtp=" + smtp + "]"; } public static class Smtp { private boolean auth; private boolean starttlsEnable; // ... getters and setters } }
创建mail.properties:
[code]mail.host=localhost mail.port=25 mail.smtp.auth=false mail.smtp.starttls-enable=false mail.from=me@localhost mail.username=duan mail.password=duan123456
上例中我们用
@ConfigurationProperties注解就可以绑定属性了。
ignoreUnknownFields = false告诉Spring Boot在有属性不能匹配到值的时候抛出异常。开发的时候很方便!
prefix用来选择哪个属性的prefix名字来绑定。
请注意setters 和 getters 需要在
@ConfigurationPropertiesbean中创建! 与
@Value注解相反。
方案1:@Component + @ConfigurationProperties
[code]package com.dxz.property3; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") public class MailProperties { private String host; private int port; private String from; private String username; private String password; private Smtp smtp; // ... getters and setters @Override public String toString() { return "MailProperties [host=" + host + ", port=" + port + ", from=" + from + ", username=" + username + ", password=" + password + ", smtp=" + smtp + "]"; } public static class Smtp { private boolean auth; private boolean starttlsEnable; } }
启动及测试类:
[code]package com.dxz.property3; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class TestProperty3 { @Autowired private MailProperties mailProperties; @RequestMapping(value = "/hello", method = RequestMethod.GET) @ResponseBody public String hello() { System.out.println("mailProperties" + mailProperties); return "hello world"; } public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty3.class).web(true).run(args); } }
结果:
[code]mailPropertiesMailProperties [host=localhost, port=25, from=me@localhost, username=duan, password=duan123456, smtp=com.dxz.property3.MailProperties$Smtp@37cebacb]
方案2:@Bean + @ConfigurationProperties
我们还可以把@ConfigurationProperties直接定义在@bean的注解上,这时bean实体类就不用@Component和@ConfigurationProperties了
[code]package com.dxz.property4; public class MailProperties { private String host; private int port; private String from; private String username; private String password; private Smtp smtp; // ... getters and setters @Override public String toString() { return "MailProperties [host=" + host + ", port=" + port + ", from=" + from + ", username=" + username + ", password=" + password + ", smtp=" + smtp + "]"; } public static class Smtp { private boolean auth; private boolean starttlsEnable; } }
启动类:
[code]@SpringBootApplication public class TestProperty4 { @Bean @ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail") public MailProperties mailProperties(){ MailProperties mp = new MailProperties(); System.out.println("zheli " + mp); return mp; } public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty4.class).web(true).run(args); } }
测试类:
[code]@RestController @RequestMapping("/task") public class TaskController { @Autowired MailProperties mailProperties; @RequestMapping(value = {"/",""}) public String hellTask(){ System.out.println("mailProperties" +mailProperties); return "hello task !!"; } }
结果:
方案3:@ConfigurationProperties + @EnableConfigurationProperties
我们和上面例子一样注解属性,然后用 Spring的
@Autowire来注入 mail configuration bean:
[code]@ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") public class MailProperties { private String host; private int port; private String from; private String username; private String password; private Smtp smtp; // ... getters and setters @Override public String toString() { return "MailProperties [host=" + host + ", port=" + port + ", from=" + from + ", username=" + username + ", password=" + password + ", smtp=" + smtp + "]"; } public static class Smtp { private boolean auth; private boolean starttlsEnable; } }
启动类及测试类:
[code]@RestController @SpringBootApplication @EnableConfigurationProperties(MailProperties.class) public class TestProperty1 { @Autowired private MailProperties mailProperties; @RequestMapping(value = "/hello", method = RequestMethod.GET) @ResponseBody public String hello() { System.out.println("mailProperties" + mailProperties); return "hello world"; } public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty1.class).web(true).run(args); } }
结果:
请注意@EnableConfigurationProperties
注解。该注解是用来开启对@ConfigurationProperties注解配置Bean的支持。也就是@EnableConfigurationProperties注解告诉Spring Boot 能支持@ConfigurationProperties。如果不指定会看到如下异常:
[code]Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dxz.property.MailProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
相关文章推荐
- spring boot @ConfigurationProperties @EnableConfigurationProperties 配置注解使用
- 在Spring Boot中使用 @ConfigurationProperties 注解
- Spring Boot2.0 @ConfigurationProperties使用详解
- 在spring-boot中使用@ConfigurationProperties
- spring boot:使用@ConfigurationProperties注解加载配置文件
- spring boot 使用@ConfigurationProperties
- 在spring-boot中使用@ConfigurationProperties
- spring boot 学习使用@ConfigurationProperties注解
- 在Spring Boot中使用 @ConfigurationProperties 注解
- spring boot 使用@ConfigurationProperties
- Spring Boot 使用@ConfigurationProperties注解获取配置文件中的值
- spring boot 使用@ConfigurationProperties
- spring boot 使用@ConfigurationPropertiesu将配置自动封装成实体类
- 在Spring Boot中使用 @ConfigurationProperties 注解
- Spring Boot中@ConfigurationProperties的使用
- 25. Spring Boot使用自定义的properties【从零开始学Spring Boot】
- 使用Log4J2.properties作为Spring Boot的日志工具
- spring boot -Properties & configuration
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- Spring Boot 之 @ConfigurationProperties