欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring boot工具類靜態(tài)屬性注入及多環(huán)境配置詳解

 更新時(shí)間:2018年04月06日 13:41:02   作者:hapjin  
這篇文章主要為大家詳細(xì)介紹了Spring boot工具類靜態(tài)屬性注入,及多環(huán)境配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

 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的配置,比如服務(wù)器地址、IP端口、數(shù)據(jù)庫名……使用一個(gè)工具類的靜態(tài)變量聲明這些配置信息,配置信息的值保存在application.yml 配置文件中。通過 @ConfigurationProperties 注入。

靜態(tài)工具類定義

屬性是靜態(tài)的:

private static String chat_username;

然后通過非靜態(tài)的 set方法注入:

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

其他類通過公有的靜態(tài)get方法獲取屬性:

public static String getChat_username() {
 return chat_username;
 }

prefix 的值在 application.yml 中定義

@ConfigurationProperties(prefix = "mongo.config")
public class MongoConfig {
 .....

整個(gè)完整代碼如下:

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 指定不同環(huán)境下使用不同的配置。active 指定激活的環(huán)境,比如 dev 或者 prod

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"


測(cè)試

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

@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 靜態(tài)變量注入配置文件

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合Druid實(shí)現(xiàn)數(shù)據(jù)庫連接池和監(jiān)控

    SpringBoot整合Druid實(shí)現(xiàn)數(shù)據(jù)庫連接池和監(jiān)控

    Druid是Java語言中使用的比較多的數(shù)據(jù)庫連接池。Druid還提供了強(qiáng)大的監(jiān)控和擴(kuò)展功能。面將介紹SpringBoot整合Druid實(shí)現(xiàn)數(shù)據(jù)庫連接池和監(jiān)控功能,感興趣的可以了解一下
    2021-08-08
  • java重試機(jī)制使用RPC必須考慮冪等性原理解析

    java重試機(jī)制使用RPC必須考慮冪等性原理解析

    這篇文章主要為大家介紹了java重試機(jī)制使用RPC必須考慮冪等性原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 基于EasyExcel實(shí)現(xiàn)百萬級(jí)數(shù)據(jù)導(dǎo)入導(dǎo)出詳解

    基于EasyExcel實(shí)現(xiàn)百萬級(jí)數(shù)據(jù)導(dǎo)入導(dǎo)出詳解

    大數(shù)據(jù)的導(dǎo)入和導(dǎo)出,相信大家在日常的開發(fā)、面試中都會(huì)遇到。本文將為大家詳細(xì)介紹一下如何利用EasyExcel實(shí)現(xiàn)百萬級(jí)數(shù)據(jù)導(dǎo)入導(dǎo)出,需要的可以參考一下
    2023-01-01
  • springmvc如何進(jìn)行異常處理

    springmvc如何進(jìn)行異常處理

    這篇文章主要介紹了springmvc如何進(jìn)行異常處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java稀疏數(shù)組的應(yīng)用實(shí)踐

    Java稀疏數(shù)組的應(yīng)用實(shí)踐

    本文主要介紹了Java稀疏數(shù)組的應(yīng)用實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Spring?@Bean?修飾方法時(shí)注入?yún)?shù)的操作方法

    Spring?@Bean?修飾方法時(shí)注入?yún)?shù)的操作方法

    對(duì)于 Spring 而言,IOC 容器中的 Bean 對(duì)象的創(chuàng)建和使用是一大重點(diǎn),Spring 也為我們提供了注解方式創(chuàng)建 bean 對(duì)象:使用 @Bean,這篇文章主要介紹了Spring?@Bean?修飾方法時(shí)如何注入?yún)?shù),需要的朋友可以參考下
    2023-10-10
  • Spring學(xué)習(xí)教程之AOP模塊的概述

    Spring學(xué)習(xí)教程之AOP模塊的概述

    AOP 從功能的角度來講,可能看作OOP編程方式的一種補(bǔ)充,提供了一種不同的代碼或者系統(tǒng)組織方式,下面這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)教程之AOP模塊的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • Java中讓界面內(nèi)的時(shí)間及時(shí)更新示例代碼

    Java中讓界面內(nèi)的時(shí)間及時(shí)更新示例代碼

    這篇文章主要給大家介紹了關(guān)于Java中讓界面內(nèi)的時(shí)間及時(shí)更新的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot 改造成https訪問的實(shí)現(xiàn)

    SpringBoot 改造成https訪問的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot 改造成https訪問的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java switch()括號(hào)內(nèi)參數(shù)的類型要求詳解

    Java switch()括號(hào)內(nèi)參數(shù)的類型要求詳解

    這篇文章主要介紹了Java switch()括號(hào)內(nèi)參數(shù)的類型要求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論