Since you need to access MongoDB, the local development environment cannot directly connect to MongoDB, you need to use the 127.0.0.2 local IP proxy through SecureCRT. However, after the program is deployed to an online production environment, it can directly access MongoDB. Therefore, after developing the program, you always need to modify the IP of the MongoDB server before submitting the code, which is very inconvenient.
private static final String PUBCHAT_HOST = "127.0.0.2";// private static final String PUBCHAT_HOST = "PROD_MONGO_SERVER_IP";
Since spring-boot-starter-data-mongodb is not used, but uses mongo-java-driver to access MongoDB, it is necessary to define some configurations to access MongoDB in the program, such as server address, IP port, database name... Use a static variable of a tool class to declare these configuration information, and the value of the configuration information is saved in the application.yml configuration file. Injected via @ConfigurationProperties.
Static tool class definition
The properties are static:
private static String chat_username;
Then inject it through the non-static set method:
@Value("${mongo.config.username}") public void setChat_username(String chat_username) { MongoConfig.chat_username = chat_username; }Other classes obtain properties through public static get methods:
public static String getChat_username() { return chat_username; }The value of prefix is defined in application.yml
@ConfigurationProperties(prefix = "mongo.config")public class 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 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 configuration file definition
Use profile to specify different configurations to use in different environments. active Specifies the activated environment, such as dev or 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"
test
Since MongoDB custom configuration is used, @SpringBootApplication(exclude = MongoAutoConfiguration.class) is used to exclude the MongoDB configuration that comes with Spring-boot.
@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()); }}Reference: spring boot static variable injection configuration file
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.