springBoot中自定义的yml文件引用的方式
生活随笔
收集整理的這篇文章主要介紹了
springBoot中自定义的yml文件引用的方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、yml配置文件
在yam文件中配置自定義的標簽
1.在yml配置文件中加入
through:url: http://10.4.2.140:49003/IBSThrough2.測試類進行測試
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
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();}總結
以上是生活随笔為你收集整理的springBoot中自定义的yml文件引用的方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 锡兵是什么意思 锡兵的解释
- 下一篇: 华为mate20pro超级快充怎么设置