【发布时间】: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