[英]SpringBoot fails to find Mongo Repository
I have a problem where I am trying to add a second mongo database to my app. 我在尝试将第二个mongo数据库添加到我的应用程序时遇到问题。 I am doing so by adding a second MongoTemplate that uses a config prefix and then a specific bean name.
我通过添加第二个使用配置前缀然后使用特定bean名称的MongoTemplate来实现。
I have an AbstractMongoConfig like so 我有一个像这样的AbstractMongoConfig
public abstract class AbstractMongoConfig {
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; }
Which I extend into a specific template config like this... To add another mongo DB I would add another one of these and then annotate different repositories to use a different config via the templateRef, right? 我可以将其扩展到这样的特定模板配置中...要添加另一个mongo DB,我将添加其中一个,然后通过templateRef注释不同的存储库以使用不同的配置,对吗?
@Configuration
@ConfigurationProperties(prefix="server.mongodb")
public class ServerMongoConfig extends AbstractMongoConfig {
@Primary
@Override
@Bean(name="serverMongoTemplate")
public MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
With this, I should be able to put configs like server.mongodb.uri=... 有了这个,我应该能够像server.mongodb.uri = ...这样的配置了。
I have a repo that uses this 我有一个使用此的回购
@Repository
public interface MAPRepository extends MongoRepository<MAP, String> {
public List<MAP> findByName(String name);
}
And the entity 和实体
@Document(collection="maptest")
public class MAP {
@Id
private String id;
private String name;
}
I then have a service class that uses this repo 然后,我有一个使用此仓库的服务类
@EnableMongoRepositories(mongoTemplateRef="serverMongoTemplate")
@Service
public class TestService {
@Autowired
@Qualifier(value = "serverMongoTemplate")
private MAPRepository mrepo;
... use mrepo ...
}
But when starting this up I get an error 但是启动时我收到一个错误
Description:
Field mrepo in testpkg.svc.TestHandler required a bean of type 'testpkg.repo.MAPRepository' that could not be found.
Action:
Consider defining a bean of type 'testpkg.repo.MAPRepository' in your configuration.
and lower ... 并降低...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'testpkg.repo.MAPRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=serverMongoTemplate)}
The service is in a different package than the repository but I have checked the imports and all that... it all matches just fine. 该服务与存储库位于不同的程序包中,但是我已经检查了导入以及所有这些内容……一切都很好。 THe MAPRepository is annotated with @Repository which means it should be available as a bean, right?
MAPRepository带有@Repository注释,这意味着它应该作为Bean可用,对吗?
What else might be going on here that is causing this to not see the bean? 还有什么可能导致这里看不到bean? Is there some class I can turn debug on to figure out what's going on?
我可以打开一些类来了解发生什么情况吗?
Before I added the ServerMongoConfig thing, it worked fine. 在添加ServerMongoConfig之前,它工作正常。 It was able to write to that DB without a problem.
它能够毫无问题地写入该数据库。 Just now it can't instantiate it properly.
刚才它无法正确实例化它。
The answer appears to be that the @EnableMongoRepositories needs to have a basePackages that refers to the proper package that contains the new template otherwise I guess mongoTemplateRef can't properly find it. 答案似乎是@EnableMongoRepositories需要具有一个basePackages,该basePackages引用包含新模板的正确软件包,否则我猜mongoTemplateRef无法正确找到它。
So, on the service class: 因此,在服务类上:
@EnableMongoRepositories(basePackages={"testpkg.repo"}, mongoTemplateRef="serverMongoTemplate")
and it all works fine. 而且一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.