导入依赖,让springboot支持@ConfigurationProperties 注解

    <!-- 支持 @ConfigurationProperties 注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>{SpringBoot的版本}</version>
</dependency>

配置文件:

connection:
username: admin
password: kyjufskifas2jsfs
remoteAddress: 192.168.1.1

配置文件注册类:

@Component
@ConfigurationProperties(prefix="connection")
public class Properties {
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;
} }

使用时直接注入即可:

@RequestMapping("/test")
@RestController
public class TestController { @Autowired
private Properties properties; @GetMapping("/test")
public void test(){
System.out.println("===================="+properties.getPassword());//====================kyjufskifas2jsfs
}
}

springboot 使用@ConfigurationProperties注入配置属性的更多相关文章

  1. SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入

    全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...

  2. SpringBoot Profiles 多环境配置及切换

    目录 前言 默认环境配置 多环境配置 多环境切换 小结 前言 大部分情况下,我们开发的产品应用都会根据不同的目的,支持运行在不同的环境(Profile)下,比如: 开发环境(dev) 测试环境(tes ...

  3. SpringBoot 属性配置文件数据注入配置和yml与properties区别

    前言 我们知道SpringBoot 通过配置类来解放一堆的xml文件配置,通属性配置文件,来进行,系统全局属性配置,这样极大的简化了我们开发过程,java web 也可以甜甜的从此 快速配置 Spri ...

  4. 使用springboot配置和注入数据源属性的方法和步骤

    /** 1.书写一个名为resources/application.properties的属性文件---->书写一个配置属性类,类名为: **/ 文件:application.propertie ...

  5. Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...

  6. Spring boot将配置属性注入到bean 专题

    https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...

  7. 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...

  8. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  9. SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...

随机推荐

  1. apache中配置php支持模块模式、cgi模式和fastcgi模式

    首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...

  2. centos编译安装php7

    环境说明 VMware 12 中搭建的CentOS 7 x64 4核 2G内存 环境中已经安装了http://blog.csdn.net/u014595668/article/details/5016 ...

  3. perceptron感知机 mtalab

    data=load('data.txt'); x=[data(:,1),data(:,2)]; y=data(:,3); k=length(y); %绘制散点图 for j=1:k if y(j)== ...

  4. MapReduce Design Patterns(chapter 2(part 1))(二)

    随着每天都有更多的数据加载进系统,数据量变得很庞大.这一章专注于对你的数据顶层的,概括性意见的设计模式,从而使你能扩展思路,但可能对局部数据是不适用的.概括性的分析都是关于对相似数据的分组和执行统计运 ...

  5. July 28th 2017 Week 30th Friday

    If equal affection cannot be, let the more loving be me. 如果没有相等的爱,那就让我爱多一点吧. There is seldom equal a ...

  6. SQL的别名和SQL的执行顺序和SQL优化

    SQL的别名 1.不可以在where子句中使用列名的别名,即select name t from emp where t>2999;是不允许的 2.使用别名的好处: 提高SQL的易读性 提高SQ ...

  7. ubuntu nginx本地局域网布署sever_name设置

    如果没有设置好sever_name 在本地输入虚拟机的ip.只会看到nginx的helloworld(打招呼界面,不可能写helloworld)界面 重点在于nginx的布署文件要加上这么一条   来 ...

  8. HDU 1079 Calendar Game (博弈论-sg)

    版权声明:欢迎关注我的博客,本文为博主[炒饭君]原创文章.未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/32336485 C ...

  9. 优酷视频上传api及demo代码

    1,优酷正常上传流程: 1). create:连接开放平台上传接口服务器,服务器端会返回upload_token以及upload_server_uri.2). create_file:连接上传服务器( ...

  10. selenium + python自动化测试unittest框架学习(三)webdriver元素操作(二)

    上一篇是元素的定位,那么定位元素的目的就是对元素进行操作,例如写入文本,点击按钮,拖动等等的操作 (1)简单元素操作 简单元素操作 find_element_by_id("kw") ...