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. 十二、LaTex中数学公式多行排版

  2. 深入理解Java的反射机制

    https://blog.csdn.net/u012585964/article/details/52011138 http://www.importnew.com/20339.html 一,java ...

  3. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

    1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

  4. 一、doT.js使用笔记

    一.赋值https://www.jianshu.com/p/19156f9fac1e <!DOCTYPE html> <html> <head> <meta ...

  5. RHEL6本地YUM源配置

    1.挂载本地光盘到系统   1)通过光驱将系统盘挂载到某个目录 [root@cluster01 ~]# mkdir   /mnt/cdrom [root@cluster01 ~]# mount -t ...

  6. stdarg的使用

    // 可变参头文件 <stdarg.h> // 主要依赖五个宏: va_list,va_start, va_arg, va_end, va_copy // 其中 va_copy 是 c99 ...

  7. vuex 管理状态

    来分析下vuex的管理状态吧,如果你用过react中的redux的管理树,那我觉得vuex对你来说很容易掌握 如果你还是不太熟悉vuex是什么,那先看下官网https://vuex.vuejs.org ...

  8. 解决 Failed to start LSB: Bring up/down networking 问题

    我用的是虚拟机,一换网络环境虚拟机的ip就没有了,重启网上就报Failed to start LSB: Bring up/down networking错误.网上查了一圈说什么HWADDR有问题,改了 ...

  9. Python 元组Ⅱ

    删除元组 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例: 以上实例元组被删除后,输出变量会有异常信息,输出如下所示: 元组运算符 与字符串一样,元组之间可以使用 + ...

  10. datatable 和实体互转

    public static class ModelConvertHelper<T> where T : class,new() { public static List<T> ...