spring boot 注入properties属性文件的值 @ConfigurationProperties
2017-11-08 00:00
411 查看
在spring boot
1.建立一个普通的bean,利用@ConfigurationProperties:
开启扫描此bean,spring application会生成一个PropertyBean
2.@ConfigurationProperties会绑定properties中的值,并且支持层级关系
3.利用
spring boot 会自动注入mail.properties中的值
mail.properties:
4.
1.建立一个普通的bean,利用@ConfigurationProperties:
@Configuration @ConfigurationProperties(locations = "classpath:properyname.properties", prefix = "datasource") public class PropertyBean { @NotBlank private String driverClassName; @NotBlank private String url; @NotBlank private String userName; ....................get、set方法
开启扫描此bean,spring application会生成一个PropertyBean
2.@ConfigurationProperties会绑定properties中的值,并且支持层级关系
@ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") public class MailProperties { public static class Smtp { private boolean auth; private boolean starttlsEnable; // ... getters and setters } @NotBlank private String host; private int port; private String from; private String username; private String password; @NotNull private Smtp smtp;
3.利用
@PropertySource注解
@Configuration @PropertySource("classpath:mail.properties") public class MailConfiguration { @Value("${mail.protocol}") private String protocol; @Value("${mail.host}") private String host; @Value("${mail.port}") private int port; @Value("${mail.smtp.auth}") private boolean auth; @Value("${mail.smtp.starttls.enable}") private boolean starttls; @Value("${mail.from}")
spring boot 会自动注入mail.properties中的值
mail.properties:
mail.host=localhost mail.port=25 mail.smtp.auth=false mail.smtp.starttls-enable=false mail.from=me@localhost mail.username= mail.password=
4.
@Configuration @PropertySource("classpath:datasource.properties") public class DataSourceConfig { @Resource private Environment env; public void getValue() { String name = env.getRequiredProperty("datasource.name"); } datasource.properties: datasource.username=root
相关文章推荐
- SpringBoot学习之配置文件注入@ConfigurationProperties
- spring boot-application.properties配置文件属性
- 【spring boot】使用@Value映射properties文件属性
- spring boot:使用@ConfigurationProperties注解加载配置文件
- springboot之读取properties属性文件
- Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。
- springboot项目接入配置中心,实现@ConfigurationProperties的bean属性刷新方案
- 【spring boot】配置文件 application.properties 属性解析
- Spring Boot 使用@ConfigurationProperties注解获取配置文件中的值
- SpringBoot属性文件配置(application.properties/yml)
- spring+mybatis 注入properties文件中属性失败导致datasource加载失败的原因和解决
- SpringBoot 注入properties文件里的值
- 【spring boot】映射properties文件属性--到Java对象
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- Spring Boot属性文件application.properties配置文档(全部)
- Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中
- Springboot属性文件application.properties配置大全,版本:1.5.4
- springboot获取配置文件中的属性(包含自定义的文件)
- spring boot 使用@ConfigurationProperties
- Spring Boot属性文件配置文档(全部)