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-Starter-Data-MongoDBは使用されていないが、Mongo-Java-Driverを使用してMongoDBにアクセスするため、サーバーアドレス、IPポート、データベース名など、プログラム内のMongoDBにアクセスする構成を定義する必要があります。 @configurationPropertiesを介して注入されます。
静的ツールクラスの定義
プロパティは静的です:
private static string chat_username;
次に、非静的セットメソッドを介して注入します。
@value( "$ {mongo.config.username}")public void setchat_username(string chat_username){mongoconfig.chat_username = chat_username; }他のクラスは、public static getメソッドを介してプロパティを取得します。
public static string getChat_username(){return chat_username; }プレフィックスの値は、application.ymlで定義されています
@configurationProperties(prefix = "mongo.config")パブリッククラスmongoconfig {.....完全なコード全体が次のとおりです。
org.springframework.beans.factory.annotation.value; Import org.springframework.boot.context.properties.configurationProperties; Import org.springframework.stereotype.component;/*** 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構成ファイル定義
プロファイルを使用して、さまざまな環境で使用するさまざまな構成を指定します。 Active DevやProdなどのアクティブ化された環境を指定します
スプリング:name:name:textml profiles:active:dev ---- spring:dev、devide、defaymo:config:config: "xxx"パスワード: "xxx"ホスト: "127.0.0.2"ポート:10001 dbname: "xxx" colprefix: "xxxx" --- spring: "xxxx" "xxxx" "xxxx" 「xxx」ホスト: "xxxx"ポート:10001 dbname: "xxxx" colprefix: "xxx"
テスト
MongoDBカスタム構成が使用されるため、@springBootApplication(exclude = mongoautoconfiguration.class)を使用して、スプリングブートに付属のMongoDB構成を除外します。
@springBootApplication(exclude = mongoautoconfiguration.class)public class application {public static void main(string [] args){springApplication.run(application.class、args); system.out.println( " - config値 - username:" + mongoconfig.getchat_username()); }}参照:Spring Boot静的変数インジェクション構成ファイル
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。