Because of the need to access MongoDB, the local development environment is not directly connected to MongoDB and requires the use of 127.0.0.2 local IP proxy via SECURECRT. But after the program is deployed to the online production environment, it is possible to directly access MongoDB, so after the development of a program, always have to modify the MongoDB server IP to submit code, this is very inconvenient.
privatestaticfinal"127.0.0.2";// private static final String PUBCHAT_HOST = "PROD_MONGO_SERVER_IP";
Instead of using Spring-boot's Spring-boot-starter-data-mongodb, you use Mongo-java-driver to access MongoDB, so you need to define some configuration to access MongoDB in your program. such as server address, IP port, database name ... These configuration information is declared with a static variable of a tool class, and the value of the configuration information is saved in a pplication.yml configuration file. by @ConfigurationProperties injection.
Static tool class definition
The property is static:
privatestatic String chat_username;
It is then injected by a non-static set method:
@Value("${mongo.config.username}") publicvoidsetChat_username(String chat_username) { MongoConfig.chat_username = chat_username; }
Other classes get properties through the public static Get method:
publicstaticgetChat_username() { return chat_username; }
The value of prefix is defined in APPLICATION.YML
@ConfigurationProperties"mongo.config")publicclass MongoConfig { .....
The entire complete code is as follows:
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 classMongoconfig {Private StaticString Chat_username;Private StaticString Chat_password;Private StaticString Chat_host;Private Static intChat_port;Private StaticString Chat_dbname;Private StaticString Chat_collprefix; Public StaticStringGetchat_username() {returnChat_username; }@Value("${mongo.config.username}") Public void Setchat_username(String chat_username) {Mongoconfig.Chat_username= Chat_username; } Public StaticStringGetchat_password() {returnChat_password; }@Value("${mongo.config.password}") Public void Setchat_password(String Chat_password) {Mongoconfig.Chat_password= Chat_password; } Public StaticStringGetchat_host() {returnChat_host; }@Value("${mongo.config.host}") Public void Setchat_host(String chat_host) {Mongoconfig.Chat_host= Chat_host; } Public Static int Getchat_port() {returnChat_port; }@Value("${mongo.config.port}") Public Static void Setchat_port(intChat_port) {Mongoconfig.Chat_port= Chat_port; } Public StaticStringGetchat_dbname() {returnChat_dbname; }@Value("${mongo.config.dbname}") Public void Setchat_dbname(String chat_dbname) {Mongoconfig.Chat_dbname= Chat_dbname; } Public StaticStringGetchat_collprefix() {returnChat_collprefix; }@Value("${mongo.config.collprefix}") Public void Setchat_collprefix(String Chat_collprefix) {Mongoconfig.Chat_collprefix= Chat_collprefix; }}
YML configuration file definition
Use profiles to specify different configurations for different environments. Active specifies an active environment, such as Dev or prod
Spring: Application: Name:TextmlProfiles: Active:Dev---Spring: Profiles:Dev, default,testMONGO: Config: Username: "XXX" Password: "XXX" Host: "127.0.0.2" Port:10001dbname: "XXX" Collprefix: "xxxx"---Spring: Profiles:ProdMONGO: Config: Username: "XXX" Password: "XXX" Host: "xxxx" Port:10001dbname: "xxxx" Collprefix: "XXX"
Test
Using the MongoDB custom configuration, use @SpringBootApplication (exclude = Mongoautoconfiguration.class) to exclude the MongoDB configuration from the Spring-boot.
@SpringBootApplication(exclude = MongoAutoConfiguration.class)publicclass Application { publicstaticvoidmain(String[] args) { SpringApplication.run(Application.class, args); System.out.println("--config value--username:" + MongoConfig.getChat_username()); }}
Reference: Spring boot static variable injection configuration file
Spring Boot Tool class static property injection and multi-environment configuration