1.通过@Value

配置文件中

wechat:
ssh:
host: 192.0.1.1
port: 22

加载类

@Component
@Data
public class SftpConfig {
@Value("${wechat.ssh.host}")
private String host;
@Value("${wechat.ssh.port}")
   private String port;
}

单元测试

package com.example.profileactivetest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigTest {
@Resource
private SftpConfig sftpConfig; @Test
public void test1(){ System.out.println(sftpConfig.getHost());
}
}

结果

run

2.@ConfigurationProperties

@Component
@Data
@ConfigurationProperties(prefix="wechat.ssh")
public class SftpConfig {
private String host;
private String port;
}

debug

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigTest {
@Resource
private SftpConfig sftpConfig; @Test
public void test1(){ System.out.println(sftpConfig.getHost());
System.out.println(sftpConfig.getPort()); }
}

3.加载数组@value = "#{'${wechat.ssh.port}'.split(',')}"

配置文件

wechat:
ssh:
host: 192.0.1.1
port: 22,23,24,25

配置类不变的话,加载出来的结果仍然是个字符

将String改为数组

@Component
@Data
@ConfigurationProperties(prefix="wechat.ssh")
public class SftpConfig {
private String host;
@Value(value = "#{'${wechat.ssh.port}'.split(',')}")
private List<String> port;
}

结果

spring boot加载自定义配置的更多相关文章

  1. spring boot 加载自定义log4j 文件路径

    spring boot 使用log4j 打印时,需首先去除自带 Logger ,然后加入log4j 依赖 <dependencies> <!-- https://mvnreposit ...

  2. Spring Boot加载配置文件

    问题1:Spring如何加载配置,配置文件位置? 1.默认位置: Spring Boot默认的配置文件名称为application.properties,SpringApplication将从以下位置 ...

  3. Spring Boot加载application.properties配置文件顺序规则

    SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...

  4. spring boot 加载web容器tomcat流程源码分析

    spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...

  5. 基于.net 的加载自定义配置-误操作

    有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件.内置的ConfigutionManager对象 是个不错的选项. 按照 app.config 的方式,做一个副本.然后从你的配置文件 ...

  6. spring boot 加载配置 文件

    在springboot启动的过程中,默契情况下会在classpath路径下加载application.properties当做系统配置文件,但有时候我们想要替换成另一个文件,可以 通过以下方式:   ...

  7. Spring boot加载REACTIVE源码分析

    一,加载REACTIVE相关自动配置 spring boot通过判断含org.springframework.web.reactive.DispatcherHandler字节文件就确定程序类型是REA ...

  8. Spring Boot 2 使用自定义配置

    在application.yml定义配置后,可以使用Environment来读取配置,也可以使用@Value注解让业务代码去读取配置.如果属性较多,可以定义属性映射对象. 开发环境:IntelliJ ...

  9. spring boot 加载原理

    spring boot quick start 在springBoot里面,很吸引的一个特征就是可以直接把应用打包成jar/war包形式.然后jar/war包可以直接运行的.不需要再配置web Ser ...

随机推荐

  1. ADF检验

    单位根检验是指检验序列中是否存在单位根,因为存在单位根就是非平稳时间序列了.单位根就是指单位根过程,可以证明,序列中存在单位根过程就不平稳,会使回归分析中存在伪回归. from statsmodels ...

  2. Linux学习--第十一天--source、环境变量目录、欢迎信息、正则、cut、awk、sed、sort、判断表达式、if、for、case、一些脚本

    source source /root/.bashrc #让修改后的配置文件在不重启系统的情况下生效.source等同于. 环境变量目录 /etc/profile /etc/profile.d/*.s ...

  3. 实现MD5算法

    using System; using System.Text; using System.Security.Cryptography; namespace Common { /// <summ ...

  4. Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities(贪心)

    E. Byteland, Berland and Disputed Cities time limit per test2 seconds memory limit per test256 megab ...

  5. 牛客假日团队赛5 K 金币馅饼 (DP 基础题)

    链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  6. Linux查看某进程的线程

    首先得知道你要查的进程的PID: 比如我要查看看我unimrcpserver的线程 第二种方法:top top命令可以实时显示各线程情况.就相当于windows的资源管理器.

  7. APIview的请求生命周期源码分析

    目录 APIview的请求生命周期源码分析 请求模块 解析模块 全局配置解析器 局部配置解析器 响应模块 异常处理模块 重写异常处理函数 渲染模块 APIview的请求生命周期源码分析 Django项 ...

  8. protocol buffer第一篇:语法介绍

    先理解一下protocol buffer是什么东西. protocol buffer是google发明的一种数据序列化方案,和json是同种类型的玩意,它非常适合在rpc场景下使用.同json一样,p ...

  9. Composite UI Application Block(CAB)

    序言 资料 https://www.cnblogs.com/lglruirui/archive/2010/06/21/1761737.html?tdsourcetag=s_pcqq_aiomsg ht ...

  10. 搭建私有git仓库gogs

    安装 gogs 下载 gogs download 安装 解压压缩包. 使用命令 cd 进入到刚刚创建的目录. 执行命令 ./gogs web,然后,就没有然后了. #后台运行 $ nohup ./go ...