我试图使用SpringBoot与Mongo数据库通信。
它正在使用spring-boot-starter-data-mongodb并自动配置一个默认bean,它允许我的MongoRepository类与数据库通信。
但是,我想覆盖默认值。我可以使用application.properties但是我需要能够在应用程序启动时将连接参数作为选项传递给命令行。
我已经尝试更改端口以打破它,我已经为Mongo配置添加了调试,无论我做什么,默认的spring配置都被使用了。它好像忽略了@Configuration注释。
我尝试了各种配置主应用程序类的方法(指定conf位置,将@Configuration添加到主类,有和没有@SpringBootApplication ......),但这里是我现在的位置.. ..
package somepackage
@EnableAutoConfiguration
@ComponentScan
public class MyApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(MyApplication.class, args);
....
}
package somepackage.conf; // should be picked up by ComponentScan, no?
@Configuration
public class MongoConf {
@Bean
public MongoClientFactoryBean mongo() throws Exception {
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
/*
setting to silly values to try to prove it is trying to create connections using this bean - expected to see errors because can't create connection... */
mongo.setHost("flibble");
mongo.setPort(345);
return mongo;
}
}
答案 0 :(得分:1)
您应该通过应用程序属性实际使用内置的Spring Boot MongoDb Starter功能和相关的自动配置。自定义主机,端口,密码等可以并且应该通过专用Spring Boot MongoDB Properties设置:
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.
并且链接到支持的属性的完整列表是here。
答案 1 :(得分:1)
除了RafalG关于MongoProperties的建议之外,我将它与ApplicationArguments类结合起来,现在我已经到了某个地方......
@Bean
@Primary
public MongoProperties mongoProperties(ApplicationArguments args) {
MongoProperties props = new MongoProperties();
String[] mongoHostAndPort = args.getSourceArgs()[3].split(":");
props.setHost(mongoHostAndPort[0]);
props.setPort(Integer.parseInt(mongoHostAndPort[1]));
return props;
}
@Bean
public MongoClientFactoryBean mongo() {
return new MongoClientFactoryBean();
}
当然要添加许多错误处理(空值,非整数等),但希望如果可以帮助其他人。
答案 2 :(得分:0)
@Configuration
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class })
@Profile("!testing")
public class TestMongoConfig extends AbstractMongoConfiguration {
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
private MongodExecutable _mongodExe;
private MongodProcess _mongod;
private MongoClient _mongo;
@Value("${spring.data.mongodb.host}")
private String host;
@Value("${spring.data.mongodb.port}")
private Integer port;
@Override
protected String getDatabaseName() {
return "test";
}
@Bean
public Mongo mongo() throws Exception {
_mongodExe = starter.prepare(new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(port, Network.localhostIsIPv6()))
.build());
_mongod = _mongodExe.start();
return new MongoClient(host, port);
}
@Override
public String getMappingBasePackage() {
return "com.test.domain";
}