springBoot中自定义的yml文件引用的方式

该博客介绍了如何在Spring框架中配置并读取YAML和properties文件中的属性。首先展示了在YAML配置文件中定义自定义标签,并通过@ConfigurationProperties和@EnableConfigurationProperties注解在测试类中注入和使用这些配置。接着,演示了读取properties配置文件的方法,包括加载配置文件,获取特定属性值,并确保资源流的正确关闭。
摘要由CSDN通过智能技术生成

一、yml配置文件

在yam文件中配置自定义的标签

1.在yml配置文件中加入

through:
  url:  http://10.4.2.140:49003/IBSThrough

2.测试类进行测试

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
//这样就可以引用配置文件中的
@Configuration
@EnableConfigurationProperties(HttpTransportProperties.class)//这个是自己写的配置类,如果是多个,就可以自己写个配置类进行配置
pulic class Test(){
@Value("${through.url}")
private String newTransportUrl;
//再写个get()set()方法
public String getNewTransportURL() {
        return newTransportURL;
    }
public void setNewTransportURL(String newTransportURL) {
        this.newTransportURL = newTransportURL;
    }
}

二、读取properties中的值

1.读取properties配置文件中的值

放到Resource目录下
config.properties

sendUrl=http//:www.wiliam.com

2.测试类进行测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Properties;
pulic class Test(){
        InputStream resourceAsStream = HttpESBPacketformerTransportConfiguration.class.getClassLoader().getResourceAsStream("config.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String throughUrl = properties.getProperty("sendUrl");
        resourceAsStream.close();
        
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值