【问题标题】:Multiple Beans of MongoTemplate with spring boot data mongodb带有spring boot数据mongodb的MongoTemplate的多个Bean
【发布时间】:2017-03-02 15:44:03
【问题描述】:

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

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

现在我也需要写入两个不同的数据库,我决定创建 MongoConfig 类
我在这里找到的示例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 bean 创建正确,取决于我的前缀属性,它很好。
但是为什么我现在不能删除这个? ${spring.data.mongodb.uri}
Mb 我做错了什么?

带有注释属性 data.mongodb.uri 的 Stacktrace。

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

【问题讨论】:

  • 能否请您发布完整的堆栈跟踪?
  • 是的,请在主帖末尾查看

标签: spring-boot spring-data autowired spring-data-mongodb mongotemplate


【解决方案1】:

数据库配置

如果您查看错误消息:

创建名为“databaseConfig”的 bean 时出错:注入 autowired 依赖失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析占位符 字符串值“${spring.data.mongodb.uri}”中的“spring.data.mongodb.uri”

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

删除它,它会起作用!

【讨论】:

  • 我忘了 DatabaseConfig 类,因为我不是作者
猜你喜欢
  • 2020-12-02
  • 2016-11-12
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 2020-12-24
  • 2018-02-05
相关资源
最近更新 更多