Da Sie auf MongoDB zugreifen müssen, kann die lokale Entwicklungsumgebung keine direkte Verbindung zu MongoDB herstellen. Sie müssen den 127.0.0.2 lokalen IP -Proxy über SecureCrt verwenden. Nachdem das Programm jedoch in einer Online -Produktionsumgebung eingesetzt wurde, kann es direkt auf MongoDB zugreifen. Nach der Entwicklung des Programms müssen Sie daher immer die IP des MongoDB -Servers ändern, bevor Sie den Code einreichen, der sehr unpraktisch ist.
private statische endgültige Zeichenfolge pubchat_host = "127.0.0.2"; // private statische endgültige String pubchat_host = "prod_mongo_server_ip";
Da Spring-Boot-Starter-Daten-MongoDB nicht verwendet wird, sondern Mongo-Java-Fahrer zum Zugriff auf MongoDB verwendet, müssen einige Konfigurationen zum Zugriff auf MongoDB im Programm definiert werden, z. Injiziert über @ConfigurationProperties.
Statische Werkzeugklassendefinition
Die Eigenschaften sind statisch:
private statische String chat_username;
Injizieren Sie es dann durch die nicht statische Set-Methode:
@Value ("$ {mongo.config.username}") public void setchat_username (string chat_username) {mongoconfig.chat_username = chat_username; }Andere Klassen erhalten Eigenschaften durch öffentliche statische Get -Methoden:
public static String getChat_username () {return chat_username; }Der Wert des Präfixs ist in application.yml definiert
@ConfigurationProperties (Präfix = "mongo.config") öffentliche Klasse Mongoconfig {.....Der gesamte vollständige Code lautet wie folgt:
import org.springFramework.beans.factory.annotation.value; import org.springframework.boot.context.properties.configurationProperties; import org.springframework.stereotype.component;/*** Erstellt von Administrator am 2018/4/4/4/4. */@Component (value = "mongoconfig")@configurationProperties (Präfix = "mongo.config") öffentliche Klasse Mongoconfig {private statische String chat_username; private statische String chat_password; private statische String chat_host; private statische int chat_port; private statische String chat_dbname; private statische 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 -Konfigurationsdateidefinition
Verwenden Sie das Profil, um verschiedene Konfigurationen anzugeben, die in verschiedenen Umgebungen verwendet werden sollen. Active gibt die aktivierte Umgebung an, wie z. B. Dev oder 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" colprefix: "xxxx" ---spring: profiles: prodmongo: config: username: "xxx" password: "xxx" Host: "xxxx" Port: 10001 dbname: "xxxx" colprefix: "xxx"
prüfen
Da die benutzerdefinierte Konfiguration von MongoDB verwendet wird, wird @springbootApplication (exklude = mongoautoconfiguration.class) verwendet, um die mit Springboot gelieferte MongoDB-Konfiguration auszuschließen.
@SpringbootApplication (exclude = mongoAutoconfiguration.class) öffentliche Klassenanwendung {public static void main (String [] args) {SpringApplication.run (application.class, args); System.out.println ("-Konfigurationswert-Benutzername:" + mongoconfig.getchat_username ()); }}Referenz: Konfigurationsdatei „Startstart" Variable Injektionskonfiguration
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.