Following a tutorial I created two connections to MongoDb databases in SpringBoot that worked perfectly, but when switching to the most recent version of the STS (Spring Tool Siute), more specifically to Spring Tool Suite
Version: 3.9.6.RELEASE Build Id: 201809180749 I get the following error:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'getMongoTemplate' threw exception; nested exception is java.lang.IllegalArgumentException: Database name must not be empty!
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 55 common frames omitted
Caused by: java.lang.IllegalArgumentException: Database name must not be empty!
at org.springframework.util.Assert.hasText(Assert.java:284) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.data.mongodb.core.MongoDbFactorySupport.<init>(MongoDbFactorySupport.java:71) ~[spring-data-mongodb-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.mongodb.core.SimpleMongoDbFactory.<init>(SimpleMongoDbFactory.java:69) ~[spring-data-mongodb-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.mongodb.core.SimpleMongoDbFactory.<init>(SimpleMongoDbFactory.java:59) ~[spring-data-mongodb-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.tutoring.web.mongo.config.AbstractMongoConfig.mongoDbFactory(AbstractMongoConfig.java:23) ~[classes/:na]
at org.tutoring.web.mongo.config.PrimaryMongoConnection.getMongoTemplate(PrimaryMongoConnection.java:28) ~[classes/:na]
at org.tutoring.web.mongo.config.PrimaryMongoConnection$$EnhancerBySpringCGLIB$$c9a41f43.CGLIB$getMongoTemplate$0(<generated>) ~[classes/:na]
at org.tutoring.web.mongo.config.PrimaryMongoConnection$$EnhancerBySpringCGLIB$$c9a41f43$$FastClassBySpringCGLIB$$f5203966.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.tutoring.web.mongo.config.PrimaryMongoConnection$$EnhancerBySpringCGLIB$$c9a41f43.getMongoTemplate(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 56 common frames omitted
and I can not find the solution. To create two connections to BD I create a class named AbstractMongoConfig that I detail:
@Data
public abstract class AbstractMongoConfig {
//Mongo DB Properties
private String host, database;
private int port;
/*
* Method that creates MongoDbFactory
* Common to both of the MongoDb connections
*/
public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(getMongoClient(), database);
}
y dos clases, una para cada BD:
1) PrimaryMongoConnection:
@Configuration
@EnableMongoRepositories(basePackages = {"org.tutoring.web.primary.repository"}, mongoTemplateRef = "primaryMongoTemplate")
@ConfigurationProperties(prefix = "primary.mongodb")
public class PrimaryMongoConnection extends AbstractMongoConfig {
@Primary
@Override
@Bean(name = "primaryMongoTemplate")
public MongoTemplate getMongoTemplate() {
return new MongoTemplate(mongoDbFactory());
}
}
private MongoClient getMongoClient() {
return new MongoClient(host, port);
}
abstract public MongoTemplate getMongoTemplate();
} and the other one called SecondaryMongoConnection that I also detail:
@Configuration
@EnableMongoRepositories(
basePackages = {"org.tutoring.web.secondary.repository"},
mongoTemplateRef = "secondaryMongoTemplate")
@ConfigurationProperties(prefix = "secondary.mongodb")
public class SecondaryMongoConnection extends AbstractMongoConfig {
@Override
public
@Bean(name = "secondaryMongoTemplate")
MongoTemplate getMongoTemplate() {
return new MongoTemplate(mongoDbFactory());
}
}
These classes load the data from the appication.properties databases as follows:
primary.mongodb.host=127.0.0.1
primary.mongodb.port=27017
primary.mongodb.database=TutoringDb
secondary.mongodb.host=127.0.0.1
secondary.mongodb.port=27017
secondary.mongodb.database=tokensDb
but from what I could verify debugging does not load the data that are in application.properties. What am I doing wrong? Something is missing? There is something that escapes me and I can not find the solution, and it took me a few days with it, I would appreciate any help