具有 spring 引导数据 mongodb 的 MongoTemplate 的多个 Bean

Multiple Beans of MongoTemplate with spring boot data mongodb

起初我没有配置就使用了MongoTemplate,
刚刚制作了@Autowired 并且 spring 根据我的 application.yml
中的属性制作了一个默认实例 属性就像:

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

现在我也需要在两个不同的数据库中编写,我决定创建 MongoConfig class
我在这里找到的示例 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;
}

并且:

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

第二个:

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

然后我把application.yml改成了:

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

当我 运行 申请时,它说我

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

当然,我删除了它,因为我不再需要它了!
为什么会这样?
然后我试着把它加回去。

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

而且有效!但是没用过!
MongoTemplate beans 创建正确,取决于我的前缀属性,它很好。
但为什么我现在不能删除它? ${spring.data.mongodb.uri}
Mb 我做错了什么?

带注释的 Stacktrace 属性 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.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

数据库配置

如果您查看错误信息:

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}"

我怀疑您有一个名为 class 的 DatabaseConfig,您自己在其中注入 属性 和 @Value("${spring.data.mongodb.uri}");

删除它,它将起作用!