简体   繁体   中英

SpringBoot mongo config with explicit template fails to start

I have a "Controller" class like

MongoController.java:

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

A mongo configuration class 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 is:

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 is just a simple class with some findBy... Nothing special.

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

application.properties:

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

When starting up it fails to start because of this:

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!

What is it complaining about? The database name isnt empty... Am I prefixing the config parameters correctly? Ultimately I'd like to have multiple MongoConfig things and either use the repository or use a @Qualifier on the mongoTemplate.

It looks like I need to override the config parameters inside AbstractMongoConfig somehow.

So, the problem is that you need plain old setters for the Abstract class. Changing it to use 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;

Further, there seems to be no way to disable the "default" mongo connection. If you set main.mongodb.uri in the config to something else, it will still try to connect to localhost anyway.

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