每日笔记---使用@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
//自动注入   
@Autowired
private User user;<br>
//方法体内获取属性值
String url=user.getUrl();<br>System.out.print(url);

每日笔记---使用@ConfigurationProperties读取yml配置的更多相关文章

  1. Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置

    从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...

  2. springboot 读取 yml 配置的几种方式

    前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...

  3. ELK 学习笔记之 elasticsearch elasticsearch.yml配置概述

    elasticsearch.yml配置概述: 设置集群名字 cluster.name 定义节点名称 node.name 节点作为master,但是不负责存储数据,只是协调. node.master: ...

  4. Springboot 2.x 无法读取yml配置值的问题:Could not resolve placeholder xxx value '${xxx}'

    最近在用Springboot2.1 新建demo工程的时候,在DataSourceConfig类中通过 @Value("${spring.datasource.url}") 的方式 ...

  5. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  6. Springboot(二)-application.yml默认的配置项以及读取自定义配置

    写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...

  7. 【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: ...

  8. SpringBoot中如何优雅的读取yml配置文件?

    YAML是一种简洁的非标记语言,以数据为中心,使用空白.缩进.分行组织数据,从而使得表示更加简洁易读.本文介绍下YAML的语法和SpringBoot读取该类型配置文件的过程. 本文目录 一.YAML基 ...

  9. 02.自定义banner、全局配置文件、@Value获取自定义配置、@ConfigurationProperties、profiles配置

    自定义banner src/main/resource 下新建 banner.txt,字符复制到banner.txt 中 生成字符网站推荐: http://patorjk.com/software/t ...

随机推荐

  1. 怎么让textarea的光标靠左对齐

    1.怎么让textarea的光标靠左对齐: 把<textarea></textarea>之间空隙去掉就可以了. 2.怎么限制textarea的字数,利用maxlength属性限 ...

  2. js截取关键字之后的字符串

    需求:截取下面字符串"="之后的所有字符 var str = "12345=6"; //要截取的字符串 var index = str.indexOf(&quo ...

  3. 【转】Javascript异步编程之setTimeout与setInterval

    Javascript异步编程之setTimeout与setInterval 转自:http://www.tuicool.com/articles/Ebueua 在谈到异步编程时,本人最主要会从以下三个 ...

  4. sqlite 时间函数及时间处理

    SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么 ...

  5. configure配置安装详解

    使用 ./configure --help 就可以查看到所有的配置选项. 1.--host=HOST指定软件运行的系统平台.如果没有指定,将会运行`config.guess'来检测.--host 指定 ...

  6. git 回滚到上个版本命令以及忽略某些文件提交

    1.git回滚到上个版本 git reset --hard FETCH_HEAD 2.git忽略某些文件的提交 以前是用默认的.gitignore 然后再里面默认某些文件不提交.但是有个问题,.git ...

  7. 个体商户POS机遭遇禁刷 职业养卡人称自有对策

    “套现猛于虎也”,这对于信用卡业而言无异于一大命门,信用卡套现金额的规模如同滚雪球般愈演愈烈.记者昨日采访银行业内了解到,虽然为防套现将根据规定关闭个体商户POS机刷信用卡的功能,但职业“养卡人”不以 ...

  8. console和windows子系统

    https://blog.csdn.net/ilvu999/article/details/8050292

  9. 个人Hadoop编程代码记录

    **WordCount package cn.cpl.recom; import java.io.IOException; import java.util.StringTokenizer; impo ...

  10. Linux下SVN安装配置及应用

    一.安装篇 centos下yum安装 yum install subversion 查看安装是否成功: svnserve --version 二.配置篇 创建svn版本库目录 mkdir /home/ ...