一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Springboot配置文件的使用

詳解Springboot配置文件的使用

2020-11-30 15:30S_H-A_N Java教程

在springboot項目中,也可以使用yml類型的配置文件代替properties文件。接下來通過本文給大家分享Springboot配置文件的使用,感興趣的朋友一起看看吧

如果使用IDEA創建Springboot項目,默認會在resource目錄下創建application.properties文件,在springboot項目中,也可以使用yml類型的配置文件代替properties文件

一、單個的獲取配置文件中的內容

在字段上使用@Value("${配置文件中的key}")的方式獲取單個的內容

1.在resource目錄下創建application.yml文件,并添加一些配置,在yml文件中,key:后面需要添加一個空格,然后是value值,假設配置如下

?
1
2
3
#注意:在yml文件中添加value值時,value前面需要加一個空格
ip: 127.0.0.0
port: 8080

2.創建一個ConfigController類,獲取配置文件中的內容并賦值給相應的字段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
 @Value("${ip}")//獲取application.yml文件中名為ip的value值
 private String ip;
 @Value("${port}")//獲取application.yml文件中名為port的value值,并且自動完成數據類型轉換
 private Integer port;
 @RequestMapping("/config")
 public String config() {
  return "ip:"+ip+",port:"+port;
 }
}

3.在SrpingbootdemoApplication中啟動項目

?
1
2
3
4
5
6
7
8
9
10
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//入口
@SpringBootApplication
public class SpringbootdemoApplication {
 public static void main(String[] args) {
  SpringApplication.run(SpringbootdemoApplication.class, args);
 }
}

4.在瀏覽器中輸入http://localhost:8080/config,可以看到輸出了配置文件中配置的內容

詳解Springboot配置文件的使用

二、使用Bean自動注入獲取配置文件中的內容

假如配置文件中有很多內容,一個一個獲取將會很麻煩,那么我們另外一種方式去獲取配置文件中的信息

1.在配置文件中添加以下信息(注意格式),此處我們使用了一個名為devconfig的前綴

?
1
2
3
devconfig:
 ip: 127.0.0.0
 port: 8080

2.創建ConfigBean,在類中添加@Componet和@ConfigurationProperties注解,其中prefix設置為devconfig,將會獲取yml中前綴為devconfig下的配置信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example;
 import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "devconfig")//獲取前綴為devconfig下的配置信息
public class ConfigBean {
 private String ip;//名字與配置文件中一致
 private Integer port;
 public String getIp() {
  return ip;
 }
 public void setIp(String ip) {
  this.ip = ip;
 }
 public Integer getPort() {
  return port;
 }
 public void setPort(Integer port) {
  this.port = port;
 }
}

3.在ConfigController中使用@Autowrite對bean自動注入,實例化bean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
// @Value("${ip}")//獲取application.yml文件中名為ip的value值
// private String ip;
//
// @Value("${port}")//獲取application.yml文件中名為port的value值,并且自動完成數據類型轉換
// private Integer port;
 //自動注入,實例化bean
 @Autowired
 private ConfigBean configBean;
 @RequestMapping("/config")
 public String config() {
  return "另一種方式: ip:"+configBean.getIp()+",port:"+configBean.getPort();
 }
}

4.運行程序,輸入http://localhost:8080/config進行測試

詳解Springboot配置文件的使用

三、多個配置文件切換使用

1.假設開發環境使用ip為:127.0.0.0 使用端口為:8080

          生產環境使用ip為:127.0.0.1 使用端口為:8081

下面來修改配置文件,在resource目錄下創建一個名為application-dev.yml文件開發環境使用配置文件和application-produce.yml生產環境配置文件

application-dev.yml

?
1
2
3
config:
 ip: 127.0.0.0
 port: 8080

application-produce.yml

?
1
2
3
config:
 ip: 127.0.0.1
 port: 8081

application.yml中配置生效的配置文件,此處設為produce,也就是使用application-produce.yml文件

?
1
2
3
spring:
 profiles:
 active: produce

2.修改ConfigBean的prefix為config

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "config")
public class ConfigBean {
 private String ip;//名字與配置文件中一致
 private Integer port;
 public String getIp() {
  return ip;
 }
 public void setIp(String ip) {
  this.ip = ip;
 }
 public Integer getPort() {
  return port;
 }
 public void setPort(Integer port) {
  this.port = port;
 }
}

3.運行程序,在瀏覽器輸入http://localhost:8080/config進行測試

詳解Springboot配置文件的使用

4.也可通過啟動jar包時添加參數來更改生效的配置文件,命令為

?
1
Java -jar XXX.jar --spring.profiles.active=poduce

以上所述是小編給大家介紹的詳解Springboot配置文件的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/lom9357bye/article/details/69697156

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲天堂视频在线观看 | 欧美大美bbb和大白屁股 | 99综合视频 | 星空无限传媒xk8027穆娜 | 513热点网 | 韩国最新理论片奇忧影院 | 猛男壮男受bl爽哭了高h | 欧美作爱福利免费观看视频 | 色偷偷91久久综合噜噜噜 | 丝袜性爱 | 97成网| 国产精品亚洲va在线观看 | a毛片久久免费观看 | 高清免费毛片 | 精品国产区一区二区三区在线观看 | 日本免费观看的视频在线 | 3p文两男一女办公室高h | 精品日韩欧美一区二区三区在线播放 | 好紧好爽再叫浪一点点潘金莲 | 国产一级黄色录像 | 大胆人gogo888体艺术在线 | 激情五色月 | 小鸟酱视频在线观看 | 99久久精品久久久久久清纯 | 免费一级国产生活片 | 日韩精品视频美在线精品视频 | 四虎国产精品免费入口 | 青丝视频免费版在线看 | 国产麻豆精品视频 | 校园全肉高h湿一女多男 | 成人影院vs一区二区 | 高h短篇合集| 精品视频在线免费观看 | 99久久国产亚洲综合精品 | 香蕉精品国产高清自在自线 | 青草热视频 | 奶茶视频有容乃大 | 欧美黑人性猛交╳xx╳动态图 | 成人精品一区二区三区 | 91啪在线观看国产在线 | 国产精品欧美亚洲韩国日本99 |