有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类

首先在配置文件里面,这些信息是这样子滴

connection.username=admin
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1

这时候我们可以定义一个实体类在装载配置文件信息

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings { private String username;
private String remoteAddress;
private String password ; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRemoteAddress() {
return remoteAddress;
}
public void setRemoteAddress(String remoteAddress) {
this.remoteAddress = remoteAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

我们还可以把@ConfigurationProperties还可以直接定义在@bean的注解上,这是bean实体类就不用@Component和@ConfigurationProperties了

@SpringBootApplication
public class DemoApplication{ //... @Bean
@ConfigurationProperties(prefix = "connection")
public ConnectionSettings connectionSettings(){
return new ConnectionSettings(); } public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

然后我们需要使用的时候就直接这样子注入

@RestController
@RequestMapping("/task")
public class TaskController { @Autowired ConnectionSettings conn; @RequestMapping(value = {"/",""})
public String hellTask(){
String userName = conn.getUsername();
return "hello task !!";
} }

如果发现@ConfigurationPropertie不生效,有可能是项目的目录结构问题,

你可以通过@EnableConfigurationProperties(ConnectionSettings.class)来明确指定需要用哪个实体类来装载配置信息

@Configuration
@EnableConfigurationProperties(ConnectionSettings.class)
public class MailConfiguration {
@Autowired private MailProperties mailProperties; @Bean public JavaMailSender javaMailSender() {
// omitted for readability
}
}

spring boot 使用@ConfigurationProperties的更多相关文章

  1. Spring Boot中@ConfigurationProperties注解实现原理源码解析

    0. 开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis ...

  2. spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件

    spring cloud  读取 配置文件属性值 1.bean @Data public class LocalFileConfig { /** * 文件存储地址 */ private String ...

  3. Spring Boot使用@ConfigurationProperties注解获取配置文件中的属性值

    注意:这种方式要提供属性的getter/setter方法—— 如果idea报错,提示没有相应的执行器,就需要在maven中添加: (虽然不配置代码也能正常运行,作用在下面会说明) 配置了该执行器后,在 ...

  4. Spring Boot配置文件详解-ConfigurationProperties和Value优缺点-(转)好文

    文章转自 http://www.cnblogs.com/itdragon/p/8686554.html Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们 ...

  5. Spring @Value注解 and Spring Boot @ConfigurationProperties注解

    一.Spring的@Value Spring EL表达式语言,支持在XML和注解中表达式,类是于JSP的EL表达式语言. 在Spring开发中经常涉及调用各种资源的情况,包含普通文件.网址.配置文件. ...

  6. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  7. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  8. Spring Boot 2.0 教程 - 配置详解

    Spring Boot 可以通过properties文件,YAML文件,环境变量和命令行参数进行配置.属性值可以通过,@Value注解,Environment或者ConfigurationProper ...

  9. Springboot 系列(二)Spring Boot 配置文件

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 不管是通过官方提供的方式获取 Spring ...

随机推荐

  1. 洛谷P4289 移动玩具 HAOI2008 搜索+状压

    正解:状压 解题报告: 先,放下传送门QwQ 说真的我jio得这题不管是思路还是实现上,都还是有一定难度的?然后就看到神仙hl博客里一句"太水了不讲了"就过掉了,,,好的趴太强辽Q ...

  2. 算法抽象及用Python实现具体算法

    一.算法抽象 它们一般是在具体算法的基础上总结.提炼.分析出来的,再反过来用于指导解决其它问题.它们适用于某一类问题的解决,用辩 证法的观点看,抽象的算法和具体的算法就是抽象与具体.普遍性与特殊性.共 ...

  3. CSS表单设计

    今天我们开始学习<十天学会web标准(div+css)>的css表单设计,包含以下内容和知识点: 改变文本框和文本域样式 用图片美化按钮 改变下拉列表样式 用label标签提升用户体验 一 ...

  4. druid部署

    Quickstart单机测试 http://druid.io/docs/0.10.1/tutorials/quickstart.html (1)Getting started 下载安装Druid: c ...

  5. POJ3009:Curling 2.0(dfs)

    http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...

  6. PAT 1054 The Dominant Color[简单][运行超时的问题]

    1054 The Dominant Color (20)(20 分) Behind the scenes in the computer's memory, color is always talke ...

  7. IE加载项

    加载项   加载项也称为ActiveX控件.浏览器扩展.浏览器帮助应用程序对象或工具栏,可以通过提供多媒体或交互式内容(如动画)来增强对网站的体验. 但是,某些加载项可导致计算机停止响应或显示不需要的 ...

  8. [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. 获取多达 16GB 的 Dropbox 免费空间!

    Dropbox官网

  10. Jmeter接口测试自动化 (三)(数据驱动测试)

    本文转载至http://www.cnblogs.com/chengtch/p/6105532.html 当我研究要通过用例优先级控制用例是否执行时,我发现了用"如果(if)控制器" ...