由於需要訪問MongoDB,但是本地開發環境不能直接連接MongoDB,需要通過SecureCRT使用127.0.0.2本地IP代理。但是程序部署到線上生產環境後,是可以直接訪問MongoDB的,因此開發好程序後,總是要修改一下MongoDB服務器的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的配置,比如服務器地址、IP端口、數據庫名……使用一個工具類的靜態變量聲明這些配置信息,配置信息的值保存在application.yml 配置文件中。通過@ConfigurationProperties 注入。
靜態工具類定義
屬性是靜態的:
private static String chat_username;
然後通過非靜態的set方法注入:
@Value("${mongo.config.username}") public void setChat_username(String chat_username) { MongoConfig.chat_username = chat_username; }其他類通過公有的靜態get方法獲取屬性:
public static String getChat_username() { return chat_username; }prefix 的值在application.yml 中定義
@ConfigurationProperties(prefix = "mongo.config")public class MongoConfig { .....整個完整代碼如下:
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
spring: application: name: textml profiles: active: dev---spring: profiles: dev, default,testmongo: config: username: "xxx" password: "xxx" host: "127.0.0.2" port: 10001 dbname: "xxx" collprefix: "xxxx"---spring: profiles: prodmongo: config: username: "xxx" password: "xxx" host: "xxxx" port: 10001 dbname: "xxxx" collprefix: "xxx"
測試
由於使用了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 靜態變量注入配置文件
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。