spring boot加载自定义配置
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加载自定义配置的更多相关文章
- spring boot 加载自定义log4j 文件路径
spring boot 使用log4j 打印时,需首先去除自带 Logger ,然后加入log4j 依赖 <dependencies> <!-- https://mvnreposit ...
- Spring Boot加载配置文件
问题1:Spring如何加载配置,配置文件位置? 1.默认位置: Spring Boot默认的配置文件名称为application.properties,SpringApplication将从以下位置 ...
- Spring Boot加载application.properties配置文件顺序规则
SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...
- spring boot 加载web容器tomcat流程源码分析
spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...
- 基于.net 的加载自定义配置-误操作
有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件.内置的ConfigutionManager对象 是个不错的选项. 按照 app.config 的方式,做一个副本.然后从你的配置文件 ...
- spring boot 加载配置 文件
在springboot启动的过程中,默契情况下会在classpath路径下加载application.properties当做系统配置文件,但有时候我们想要替换成另一个文件,可以 通过以下方式: ...
- Spring boot加载REACTIVE源码分析
一,加载REACTIVE相关自动配置 spring boot通过判断含org.springframework.web.reactive.DispatcherHandler字节文件就确定程序类型是REA ...
- Spring Boot 2 使用自定义配置
在application.yml定义配置后,可以使用Environment来读取配置,也可以使用@Value注解让业务代码去读取配置.如果属性较多,可以定义属性映射对象. 开发环境:IntelliJ ...
- spring boot 加载原理
spring boot quick start 在springBoot里面,很吸引的一个特征就是可以直接把应用打包成jar/war包形式.然后jar/war包可以直接运行的.不需要再配置web Ser ...
随机推荐
- 三、Vue CLI-单页面
一.单页面 代码如下: <template> <div class="header">{{title}}</div> </template ...
- 【转】sysctl命令及改变net.ipv4.ip_forward = 1方法
转自:https://blog.csdn.net/michaelzhou224/article/details/16979285 sysctl配置与显示在/proc/sys目录中的内核参数.可以用sy ...
- php关于jquery ajax json不返回数据的问题
分析原因: 1.php端代码有错导致echo输出错误,导致ajax返回函数不执行 2.jquery版本原因 3.juqery前端script代码问题
- c字符串函数
1. bcmp(3) 类ma似于strncmp(3) 但是比较结果不一定是两个字符的ascii码之差. 返回值:相等0,不相等非零(不一定是-1) 2.bcopy(3)类ma似于strncpy(3) ...
- 如何使用Beyond Compare比较两个文件夹的差异
很多时候,我们需要比较两个文件或者两个文件夹的差异性,看看是哪里不同.这时候就需要一款比较软件来处理,Beyond Compare就是其中一款非常好用的版本比较工具,下面简单介绍一下Beyond Co ...
- SpringMVC @CookieValue注解
@CookieValue的作用 用来获取Cookie中的值 @CookieValue参数 1.value:参数名称 2.required:是否必须 3.defaultValue:默认值 @Cookie ...
- java——SimpleDateFormat与DateTimeFormatter
https://www.jianshu.com/p/b212afa16f1f SimpleDateFormat不是线程安全的 DateTimeFormatter是线程安全的
- 在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了
在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了 解决方法: 1.运行:输入services.msc进入服务 2.找到(前提你的P ...
- 【shell】文本按行逆序
1.最简单的方法是使用tac [root ~]$ seq |tac 2.使用tr和awk. tr把换行符替换成自定义的分隔符,awk分解替换后的字符串,并逆序输出 [root ~]$ seq | tr ...
- 爬虫之Handler处理器和自定义Opener
一.Handler处理器和自定义Opener opener是 request.OpenerDirector 的实例,我们之前一直都在使用的urlopen,它是一个特殊的opener(也就是模块帮我们构 ...