1.通过@Value
配置文件中
customization: sftp: host: 192.0.1.1 port: 22 username: test password: 123456
加载类
@Component @Data public class SftpConfig { @Value("${customization.sftp.host}") private String host; @Value("${customization.sftp.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; /** * @Title: * @Auther: pujiahong * @Date: 2019/9/11 9:44 * @Version: 1.0 * @Description: */ @RunWith(SpringRunner.class) @SpringBootTest public class ConfigTest { @Resource private SftpConfig sftpConfig; @Test public void test1(){ System.out.println(sftpConfig.getHost()); } }
结果

run,或者debug


原文:https://www.cnblogs.com/pu20065226/p/11505228.html