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

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

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

服務器之家 - 編程語言 - Java教程 - 詳解Spring Boot讀取配置文件與配置文件優先級

詳解Spring Boot讀取配置文件與配置文件優先級

2021-05-26 12:19良辰美景TT Java教程

這篇文章主要介紹了詳解Spring Boot讀取配置文件與配置文件優先級,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring boot讀取配置文件

1)通過注入applicationcontext 或者 environment對象來讀取配置文件里的配置信息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ivan.config.controller;
 
import org.springframework.beans.factory.annotation.autowired;
 
import org.springframework.context.applicationcontext;
 
import org.springframework.core.env.environment;
 
import org.springframework.web.bind.annotation.requestmapping;
 
import org.springframework.web.bind.annotation.requestmethod;
 
import org.springframework.web.bind.annotation.restcontroller;
 
@restcontroller
 
public class configcontroller {
  @autowired
 
  applicationcontext context;
 
  @autowired
 
  environment environment;
 
  @requestmapping(value="/config", method={requestmethod.get})
 
  public string getconfigcontent(){      
 
    string name = context.getenvironment().getproperty("db.user.name");
 
    return name;
 
  }
 
  @requestmapping(value="/configenv", method={requestmethod.get})
 
  public string getconfigenvironment(){
 
    string name = environment.getproperty("db.user.name");
 
    return name;
 
  }
}

2)通過@configurationproperties配合@propertysource讀取配置文件里的配置信息。

1:通過@propertysource指定當前類里屬性的配置文件地址,configurationproperties可以指定配置的前綴,@configuration用于定義一個配置類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.ivan.config.entity;
import org.springframework.boot.context.properties.configurationproperties;
 
import org.springframework.context.annotation.configuration;
 
import org.springframework.context.annotation.propertysource;
 
@configuration
 
@propertysource("classpath:config/druid.properties")
 
@configurationproperties(prefix = "druid")
 
public class druidconfig {
 
  private int  initialsize;
 
  private int  minidle;
 
  private int  maxactive;
 
  private int  maxwait;
 
  private string validationquery;
 
  private boolean testwhileidle;
 
  private boolean testonborrow;
 
  private boolean testonreturn;
 
  public int getinitialsize() {
 
    return initialsize;
 
  }
 
  public void setinitialsize(int initialsize) {
 
    this.initialsize = initialsize;
 
  }
 
  public int getminidle() {
 
    return minidle;
 
  }
 
  public void setminidle(int minidle) {
 
    this.minidle = minidle;
 
  }
 
  public int getmaxactive() {
 
    return maxactive;
 
  }
 
  public void setmaxactive(int maxactive) {
 
    this.maxactive = maxactive;
 
  }
 
  public int getmaxwait() {
 
    return maxwait;
 
  }
 
  public void setmaxwait(int maxwait) {
 
    this.maxwait = maxwait;
 
  }
 
  public string getvalidationquery() {
 
    return validationquery;
 
  }
 
  public void setvalidationquery(string validationquery) {
 
    this.validationquery = validationquery;
 
  }
 
  public boolean istestwhileidle() {
 
    return testwhileidle;
 
  }
 
  public void settestwhileidle(boolean testwhileidle) {
 
    this.testwhileidle = testwhileidle;
 
  }
 
  public boolean istestonborrow() {
 
    return testonborrow;
 
  }
 
  public void settestonborrow(boolean testonborrow) {
 
    this.testonborrow = testonborrow;
 
  }
 
  public boolean istestonreturn() {
 
    return testonreturn;
 
  }
 
  public void settestonreturn(boolean testonreturn) {
 
    this.testonreturn = testonreturn;
 
  }
 
  @override
 
  public string tostring() {
 
    return "druidconfig [initialsize=" + initialsize + ", minidle=" + minidle + ", maxactive=" + maxactive + ", maxwait=" + maxwait + ", validationquery=" + validationquery + ", testwhileidle=" + testwhileidle + ", testonborrow=" + testonborrow + ", testonreturn=" + testonreturn + "]";
 
  }
}

2:對應的配置文件:

?
1
2
3
4
5
6
7
8
druid.initialsize=5
druid.minidle=5
druid.maxactive=20
druid.maxwait=60000
druid.validationquery=select 'x'
druid.testwhileidle=true
druid.testonborrow=true
druid.testonreturn=true

3:在需要用到的類通過@autowired注入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import com.ivan.config.entity.druidconfig;
@restcontroller
public class druidconfigcontroller {
  @autowired
  public druidconfig druidconfig;
 
  @requestmapping(value="/druidconfig", method={requestmethod.get})
  public string getdruidconfig(){
    return druidconfig.tostring();
  }
}

3)通過@value注解

1:需要得到配置屬性的類如下,可以在任何需要得到配置的地方用@value注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ivan.config.entity;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.configuration;
@configuration
 
public class valuetest {
  @value("${db.user.name}")
  private string username;
  public string getusername() {
    return username;
  }
  public void setusername(string username) {
    this.username = username;
  }
}

2:測試controller類通過@autowired注入實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import com.ivan.config.entity.valuetest;
@restcontroller
public class valuecontroller {
  @autowired
  private valuetest value;
 
  @requestmapping(value="/configvalue", method={requestmethod.get})
  public string getconfig(){
    return value.getusername();
  }
}

spring boot 配置文件優先級:

1:命令行參數。(以--開頭的參數,比如可以設置:--server.port對同一套代碼設置不同的參數)
2: 通過 system.getproperties() 獲取的 java 系統參數。
3:操作系統環境變量(這解釋了為什么你通過application.properties設置的user.name取的是系統的用戶名了)
4:從 java:comp/env 得到的 jndi 屬性。
5: 應用 jar 文件之外的屬性文件(系統的application.properties文件)
6:應用 jar 文件內部的屬性文件。
7: 在應用配置 java 類(包含“@configuration”注解的 java 類)中通過“@propertysource”注解聲明的屬性文件。
8: 通過“springapplication.setdefaultproperties”聲明的默認屬性。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/6fcd33551532

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区三区丶四区 | 亚洲高清视频网站 | 女高h | 国产亚洲福利一区二区免费看 | 97国产蝌蚪视频在线观看 | 国产在线视频一区二区三区 | 久久草福利自拍视频在线观看 | 亚欧日韩| 国产九九视频在线观看 | 亚洲欧美专区精品伊人久久 | 二次元美女内裤凹陷太深 | 色姑娘色综合 | 日本暖暖在线视频 | 亚洲成年人免费网站 | 亚洲色图欧美偷拍 | 欧美a在线观看 | 日本中文字幕一区二区有码在线 | 91制片厂制作传媒破解版免费 | 国产成人免费高清激情视频 | 九九精品视频在线免费观看 | 国产精品拍拍拍福利在线观看 | 日本xxx片免费高清在线 | 欧美日韩精品在线观看 | 农村老少伦小说 | 亚洲国产在 | 亚洲丁香网 | 歪歪私人影院成人毛片 | 经典欧美gifxxoo动态图暗网 | 亚洲品质自拍网站 | 大jjjj免费看视频 | 成在线人免费视频一区二区三区 | 亚洲精品国产福利片 | 色综合综合色 | 亚洲精品网址 | 波多野结衣同性系列698 | 息与子中文字幕bd | 亚洲一区二区三区免费视频 | 黑人好大好硬好深好爽想要h | 日韩精品1 | 荡女人人爱全文免费阅读 | 色啪久久婷婷综合激情 |