首页 > 编程语言 > 详细

spring boot加载自定义配置

时间:2019-09-11 12:10:48      阅读:110      评论:0      收藏:0      [点我收藏+]

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

 技术分享图片

 

 技术分享图片

 

spring boot加载自定义配置

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!