我认为您可以使用Spring配置文件。
这是文档。
Spring Profiles提供了一种分离应用程序配置部分并使之仅在某些环境中可用的方法。
更新
注意: 我上面提到的文档中指示了我下面将要讨论的所有内容……您应该真正看一下本文档。这个文档很棒(不要开玩笑)。
从附录A.通用应用程序属性(Spring启动文档)
这是在中配置远程MongoDB实例的方法
application.properties:
# MonGODB (MongoProperties)spring.data.mongodb.authentication-database= # Authentication database name.spring.data.mongodb.database=test # Database name.spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.spring.data.mongodb.grid-fs-database= # GridFS database name.spring.data.mongodb.host=localhost # Mongo server host.spring.data.mongodb.password= # Login password of the mongo server.spring.data.mongodb.port=27017 # Mongo server port.spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. When set, host and port are ignored.spring.data.mongodb.username= # Login user of the mongo server.
这是在中配置嵌入式MongoDB实例的方法
application.properties:
# EMBEDDED MonGODB (EmbeddedMongoProperties)spring.mongodb.embedded.features=SYNC_DELAY # Comma-separated list of features to enable.spring.mongodb.embedded.storage.databaseDir= # Directory used for data storage.spring.mongodb.embedded.storage.oplogSize= # Maximum size of the oplog in megabytes.spring.mongodb.embedded.storage.replSetName= # Name of the replica set.spring.mongodb.embedded.version=2.6.10 # Version of Mongo to use.
从更改配置取决于环境(spring启动文件)
要对属性文件执行相同的操作,可以使用
application-${profile}.properties指定特定于配置文件的值。
您可以将MongoDB嵌入式配置定义为
application-dev.properties,将MongoDB远程配置定义为
application-prod.properties
更新二:回归
我假设您在(从document)类中启动嵌入式MongoDB实例:
import de.flapdoodle.embed.mongo.config.ArtifactStoreBuilder; ... MongodStarter starter = MongodStarter.getDefaultInstance(); String bindIp = "localhost"; int port = 12345; IMongodConfig mongodConfig = new MongodConfigBuilder() .version(Version.Main.PRODUCTION) .net(new Net(bindIp, port, Network.localhostIsIPv6())) .build(); MongodExecutable mongodExecutable = null;
您可以为此类分配spring概要文件,例如(来自document):
@Configuration@Profile("dev")public class ProductionConfiguration { // ...}这样,仅当您选择
dev概要文件时,才会启动嵌入式MongoDB 。



