gpt4 book ai didi

java - 带有显式模板的 Spring Boot mongo 配置无法启动

转载 作者:行者123 更新时间:2023-12-02 11:38:15 25 4
gpt4 key购买 nike

我有一个像这样的“ Controller ”类

MongoController.java:

@EnableMongoRepositories(mongoTemplateRef="mainMongoTemplate")
@RestController
public class MongoController {
@Autowired
MongoPersonRepository mrepo;

mongo配置类MainMongoConfig.java

@Configuration
@ConfigurationProperties(prefix = "main.mongodb")
public class MainMongoConfig extends AbstractMongoConfig {
@Primary
@Override
@Bean(name="mainMongoTemplate")
public MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}

AbstractMongoConfig 是:

public abstract class AbstractMongoConfig {
private String host;
private int port;
private String database;

public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(host, port), database);
}

abstract public MongoTemplate getMongoTemplate() throws Exception;
}

MongoPersonRepository 只是一个简单的类,带有一些 findBy...没什么特别的。

public interface MongoPersonRepository extends MongoRepository<MongoPerson, String> {
public MongoPerson findByName(String name);
public List<MongoPerson> findByAge(int age);
}

应用程序属性:

main.mongodb.uri=mongodb://localhost:27017/main

启动时无法启动,原因如下:

2018-02-13 13:44:59.918  WARN 32194 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'mongoController': Unsatisfied dependency expressed through field 'mrepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoPersonRepository': Cannot resolve reference to bean 'mainMongoTemplate'
while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainMongoTemplate' defined in class path resource [hello/mongo/MainMongoConfig.class]: Bean instantiation via fac
tory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'getMongoTemplate' threw exception; nested exception is java.lang.IllegalArgumentEx
ception: Database name must not be empty!

它在提示什么?数据库名称不为空...我是否正确地为配置参数添加了前缀?最终,我希望拥有多个 MongoConfig 事物,并使用存储库或在 mongoTemplate 上使用 @Qualifier。

看起来我需要以某种方式覆盖 AbstractMongoConfig 中的配置参数。

最佳答案

所以,问题是您需要抽象类的普通旧 setter 。将其更改为使用 URI:

private String uri;

public void setUri(String uri) {
this.uri = uri;
}

public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClientURI(uri));
}

abstract public MongoTemplate getMongoTemplate() throws Exception;

此外,似乎没有办法禁用“默认”mongo 连接。如果您在配置中将 main.mongodb.uri 设置为其他内容,它仍然会尝试连接到本地主机。

关于java - 带有显式模板的 Spring Boot mongo 配置无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48773708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com