每日笔记---使用@ConfigurationProperties读取yml配置
每日笔记---使用@ConfigurationProperties读取yml配置
参考地址 https://www.cnblogs.com/mycs-home/p/8352140.html
1.添加pom依赖
|
1
2
3
4
5
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional></dependency> |
2.application.yml文件中添加需要配置的属性,注意缩进
|
1
2
3
4
5
|
Myyml: username: cs password: 123456 url: jdbc:mysql://localhost:3306/test driver: com.mysql.jdbc.Driver |
3.新建一个类,@Component注解表明是组件,可被自动发现,@ConfigurationProperties注解之前是location属性表明配置文件位置,prefix表示读取的配置信息的前缀,但新版本中废除了location属性(网上说是1.5.2之后),故只写前缀,默认读取application.yml中数据。重点!!一定要在这个类中写getter和setter,否则配置中的属性值无法自动注入
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.cs.background.util;import lombok.ToString;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "Myyml")public class User{ //数据库连接相关 private String url; private String driver; private String username; private String password; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }} |
4.Controller类中执行自动注入,获取属性
|
1
2
3
4
5
|
//自动注入 @Autowiredprivate User user;<br>//方法体内获取属性值String url=user.getUrl();<br>System.out.print(url); |
每日笔记---使用@ConfigurationProperties读取yml配置的更多相关文章
- Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置
从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...
- springboot 读取 yml 配置的几种方式
前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...
- ELK 学习笔记之 elasticsearch elasticsearch.yml配置概述
elasticsearch.yml配置概述: 设置集群名字 cluster.name 定义节点名称 node.name 节点作为master,但是不负责存储数据,只是协调. node.master: ...
- Springboot 2.x 无法读取yml配置值的问题:Could not resolve placeholder xxx value '${xxx}'
最近在用Springboot2.1 新建demo工程的时候,在DataSourceConfig类中通过 @Value("${spring.datasource.url}") 的方式 ...
- 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...
- Springboot(二)-application.yml默认的配置项以及读取自定义配置
写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...
- 【spring boot】使用注解@ConfigurationProperties读取配置文件时候 报错 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependenc
如题,配置文件如下: #注册中心配置 eureka: instance: instanceId: ${spring.application.name}:${random.int} hostname: ...
- SpringBoot中如何优雅的读取yml配置文件?
YAML是一种简洁的非标记语言,以数据为中心,使用空白.缩进.分行组织数据,从而使得表示更加简洁易读.本文介绍下YAML的语法和SpringBoot读取该类型配置文件的过程. 本文目录 一.YAML基 ...
- 02.自定义banner、全局配置文件、@Value获取自定义配置、@ConfigurationProperties、profiles配置
自定义banner src/main/resource 下新建 banner.txt,字符复制到banner.txt 中 生成字符网站推荐: http://patorjk.com/software/t ...
随机推荐
- 数组之reduce()和reduceRight()
1.reduce()和reduceRight()方法使用指定的函数将数组元素进行组合,生成单个值. reduce()可以传入两个参数,第一个是执行化简操作的函数.同样这个函数可以有参数,第一个参数代表 ...
- <Android 应用 之路> MPAndroidChart~LineChart
简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...
- JavaEE中表现层、持久层、业务层的职责分析(转载)
表现层.持久层.业务层 注:本文转载于:http://www.blogjava.net/jiabao/archive/2007/04/08/109189.html 为了实现web层(struts)和持 ...
- Android通过浏览器打开app页面并且传递值
最近公司有个需求,要求从第三方网页端打开一个网页,然后在网页中点击“下载”,“打开”按钮,在app端进行下载和打开操作.这里记录下方法. 首先,网页和app页面进行交互,其实会很快想到JS交互,但是现 ...
- 删除List中指定的元素
删除List中指定的元素 CopyOnWriteArrayList是ArrayList的一个线程安全的变体实现,即可在多线程并发环境中使用 List<Map<String, Object& ...
- arm汇编学习(三)
一.ndk编译android上运行的c程序 新建个hello目录,底下要有jni目录,下面就是Android.mk文件 1.Android.mk文件内容如下: LOCAL_PATH:= $(call ...
- 20.远程分支&跟踪分支
远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...
- 如何为Android平台编译 opencv3 和 opencv_contrib (Linux)
编译出来的opencv库有问题,正在调试中 ...... 本文以编译opencv 3.3.0 和 opencv_contrib 3.3.0为例,系统为 Linux x64 (Fedora 21),具体 ...
- centos7和centos6区别
CentOS 7 vs CentOS 6的不同 (1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统[CentOS6 ...
- how to do error handing with WCF by using attributes to log your errors z
There are multiple ways to do error handling in WCF as listed by Pedram Rezaei Blog. The default way ...