springBoot中自定义的yml文件引用的方式
生活随笔
收集整理的這篇文章主要介紹了
springBoot中自定义的yml文件引用的方式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、yml配置文件
在yam文件中配置自定義的標(biāo)簽
1.在yml配置文件中加入
through:url: http://10.4.2.140:49003/IBSThrough2.測(cè)試類進(jìn)行測(cè)試
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; //這樣就可以引用配置文件中的 @Configuration @EnableConfigurationProperties(HttpTransportProperties.class)//這個(gè)是自己寫(xiě)的配置類,如果是多個(gè),就可以自己寫(xiě)個(gè)配置類進(jìn)行配置 pulic class Test(){ @Value("${through.url}") private String newTransportUrl; //再寫(xiě)個(gè)get()set()方法 public String getNewTransportURL() {return newTransportURL;} public void setNewTransportURL(String newTransportURL) {this.newTransportURL = newTransportURL;} }二、讀取properties中的值
1.讀取properties配置文件中的值
放到Resource目錄下
config.properties
2.測(cè)試類進(jìn)行測(cè)試
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();}總結(jié)
以上是生活随笔為你收集整理的springBoot中自定义的yml文件引用的方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 锡兵是什么意思 锡兵的解释
- 下一篇: 华为mate20pro超级快充怎么设置