Konsta
Konsta

Reputation: 85

Multiple Beans of MongoTemplate with spring boot data mongodb

At first i used MongoTemplate without Configuration,
just made @Autowired and spring makes one default instanse based on properties from my application.yml
properties was like:

spring:
  profiles: default
  data:
    mongodb:
      uri: mongodb://localhost:27017/myDbName

Now i need too write in two different database, i desided to create MongoConfig class
Example i found here Example

public abstract class AbstractMongoConfig {

private String host;

private int port;

private String database;

//here is Getters and Setters...

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

abstract public MongoTemplate getMongoTemplate() throws Exception;
}

And:

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

Second one:

@Configuration
@ConfigurationProperties(prefix = "second.mongodb")
public class SecondaryMongoConfig
        extends  AbstractMongoConfig {
    @Override public @Bean(name = "secondMongoTemplate")
    MongoTemplate getMongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}

Then i changed application.yml to:

spring:
  profiles: default
primary:
  mongodb:
    host: localhost
    port: 27017
    database: myDbName
second:
  mongodb:
    host: localhost
    port: 27017
    database: myDbName2

When i run application, it says me

IllegalArgumentException:Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"

of course, i deleted it, because i dont need it any more!
Why so?
Then i tried add it back.

spring:
  profiles: default
  data:
    mongodb:
      uri: mongodb://localhost:27017/fake #notUsedActually
vkad:
  mongodb:
    host: localhost
    port: 27017
    database: myDbName
gap:
  mongodb:
    host: localhost
    port: 27017
    database: myDbName2

And it works! But dont used!
MongoTemplate beans creates right, depends on my prefix properties, its good.
But why i cant delete this for now? ${spring.data.mongodb.uri}
Mb im doing smth wrong ?

Stacktrace with commented property data.mongodb.uri.

2016-10-24 13:06:29.372  WARN 15016 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"
2016-10-24 13:06:29.377  INFO 15016 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:813) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1039) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    ... 21 common frames omitted

Upvotes: 1

Views: 4911

Answers (1)

alexbt
alexbt

Reputation: 17045

DatabaseConfig

If you look at the error message:

Error creating bean with name 'databaseConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"

I suspect you have a class named DatabaseConfig in which you are yourself injecting the property with @Value("${spring.data.mongodb.uri}");

Remove this and it will work!

Upvotes: 1

Related Questions