国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

服務器之家 - 編程語言 - Java教程 - Spring boot工具類靜態屬性注入及多環境配置詳解

Spring boot工具類靜態屬性注入及多環境配置詳解

2021-04-20 14:42hapjin Java教程

這篇文章主要為大家詳細介紹了Spring boot工具類靜態屬性注入,及多環境配置詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下

由于需要訪問MongoDB,但是本地開發環境不能直接連接MongoDB,需要通過SecureCRT使用127.0.0.2本地IP代理。但是程序部署到線上生產環境后,是可以直接訪問MongoDB的,因此開發好程序后,總是要修改一下MongoDB服務器的IP才能提交代碼,這樣很是不方便。

?
1
2
private static final String PUBCHAT_HOST = "127.0.0.2";
// private static final String PUBCHAT_HOST = "PROD_MONGO_SERVER_IP";

由于沒有使用spring-boot自帶的 spring-boot-starter-data-mongodb ,而是使用 mongo-java-driver 訪問MongoDB,因此在程序中需要定義一些訪問MongoDB的配置,比如服務器地址、IP端口、數據庫名……使用一個工具類的靜態變量聲明這些配置信息,配置信息的值保存在application.yml 配置文件中。通過 @ConfigurationProperties 注入。

靜態工具類定義

屬性是靜態的:

?
1
private static String chat_username;

然后通過非靜態的 set方法注入:

?
1
2
3
4
@Value("${mongo.config.username}")
 public void setChat_username(String chat_username) {
 MongoConfig.chat_username = chat_username;
 }

其他類通過公有的靜態get方法獲取屬性:

?
1
2
3
public static String getChat_username() {
 return chat_username;
 }

prefix 的值在 application.yml 中定義

?
1
2
3
@ConfigurationProperties(prefix = "mongo.config")
public class MongoConfig {
 .....

整個完整代碼如下:

?
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
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * Created by Administrator on 2018/4/4.
 */
@Component(value = "MongoConfig")
@ConfigurationProperties(prefix = "mongo.config")
public class MongoConfig {
 
 private static String chat_username;
 
 private static String chat_password ;
 
 private static String chat_host;
 
 private static int chat_port;
 
 private static String chat_dbname;
 
 private static String chat_collprefix;
 
 public static String getChat_username() {
 return chat_username;
 }
 
 @Value("${mongo.config.username}")
 public void setChat_username(String chat_username) {
 MongoConfig.chat_username = chat_username;
 }
 
 public static String getChat_password() {
 return chat_password;
 }
 
 @Value("${mongo.config.password}")
 public void setChat_password(String chat_password) {
 MongoConfig.chat_password = chat_password;
 }
 
 public static String getChat_host() {
 return chat_host;
 }
 
 @Value("${mongo.config.host}")
 public void setChat_host(String chat_host) {
 MongoConfig.chat_host = chat_host;
 }
 
 public static int getChat_port() {
 return chat_port;
 }
 
 @Value("${mongo.config.port}")
 public static void setChat_port(int chat_port) {
 MongoConfig.chat_port = chat_port;
 }
 
 public static String getChat_dbname() {
 return chat_dbname;
 }
 
 @Value("${mongo.config.dbname}")
 public void setChat_dbname(String chat_dbname) {
 MongoConfig.chat_dbname = chat_dbname;
 }
 
 public static String getChat_collprefix() {
 return chat_collprefix;
 }
 
 @Value("${mongo.config.collprefix}")
 public void setChat_collprefix(String chat_collprefix) {
 MongoConfig.chat_collprefix = chat_collprefix;
 }
}

yml配置文件定義

通過 profile 指定不同環境下使用不同的配置。active 指定激活的環境,比如 dev 或者 prod

?
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
spring:
 application:
 name: textml
 profiles:
 active: dev
 
 
---
spring:
 profiles: dev, default,test
 
mongo:
 config:
  username: "xxx"
  password: "xxx"
  host: "127.0.0.2"
  port: 10001
  dbname: "xxx"
  collprefix: "xxxx"
 
---
spring:
 profiles: prod
mongo:
 config:
  username: "xxx"
  password: "xxx"
  host: "xxxx"
  port: 10001
  dbname: "xxxx"
  collprefix: "xxx"

測試

由于使用了MongoDB自定義配置,故使用 @SpringBootApplication(exclude = MongoAutoConfiguration.class) 排除Spring-boot 中自帶的MongoDB配置。

?
1
2
3
4
5
6
7
8
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class Application {
 
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 System.out.println("--config value--username:" + MongoConfig.getChat_username());
 }
}

參考:spring boot 靜態變量注入配置文件

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

原文鏈接:https://www.cnblogs.com/hapjin/p/8719213.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩精品一区二区在线观看 | 久久久久无码国产精品一区 | 国产日韩视频 | 一区二区三区在线看 | 色在线视频| 操操网站| 久久99视频 | 操av网 | 亚洲啊v | 欧美成人毛片 | 青青操av在线 | 中文字幕一区二区三区在线观看 | 午夜av一区二区 | 欧美日韩一区二区在线观看 | 羞羞视频免费观看网站 | 99re6在线视频精品免费 | 欧美日本韩国一区二区 | 黄色三及毛片 | 欧美日韩在线观看一区二区 | 精品久久久久久久久久 | 久久国产精品视频 | 日韩一区二区三区在线 | 久久美女 | 亚洲一区免费 | 99精品一区 | 欧美视频在线观看免费 | 日韩精品无码一区二区三区 | 天堂中文在线视频 | 欧美片网站免费 | 国产成人激情 | 在线视频91| 午夜免费小视频 | 久久精品国产一区二区三区 | 国产激情一区二区三区 | 黄色美女网站在线观看 | 久久久久午夜 | av毛片免费看 | 天天爽天天操 | 综合在线视频 | 亚洲精品在线视频观看 | 国产黄色a级毛片 |