简体   繁体   中英

SpringBoot fails to find Mongo Repository

I have a problem where I am trying to add a second mongo database to my app. I am doing so by adding a second MongoTemplate that uses a config prefix and then a specific bean name.

I have an AbstractMongoConfig like so

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?

@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=...

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?

What else might be going on here that is causing this to not see the 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. 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.

So, on the service class:

@EnableMongoRepositories(basePackages={"testpkg.repo"}, mongoTemplateRef="serverMongoTemplate")

and it all works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM