我正在尝试使用@EnableMongoRepositories来使用两个独立的mongo存储库,如:
@Configuration
@EnableMongoRepositories(mongoTemplateRef = "mongoBOTemplate", basePackages = "sandbox.dao.bo")
public class BOMongoConfig {
@Value("#{mongo.hostBO}")
private String hostBO;
@Value("#{mongo.databaseBO}")
private String databaseBO;
@Bean
public MongoDbFactory mongoBODbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(hostBO), databaseBO);
}
@Bean
public MongoTemplate mongoBOTemplate() throws Exception {
return new MongoTemplate(mongoBODbFactory());
}
}
和
@Configuration
@EnableMongoRepositories(mongoTemplateRef = "mongoTemplate", basePackages = "sandbox.dao.sandbox")
public class SandboxMongoConfig {
@Value("#{mongo.host}")
private String host;
@Value("#{mongo.database}")
private String database;
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(host), database);
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
但是我因为这个错误而感到困惑:
710 [RMI TCP Connection(2)-127.0.0.1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
java.lang.IllegalArgumentException: Environment must not be null!
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.data.repository.config.RepositoryConfigurationSourceSupport.<init>(RepositoryConfigurationSourceSupport.java:50)
at org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource.<init>(AnnotationRepositoryConfigurationSource.java:74)
at org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport.registerBeanDefinitions(RepositoryBeanDefinitionRegistrarSupport.java:74)
at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:340)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:233)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:154)
at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:349)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:233)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:154)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:140)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:282)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
据我所知,只有一个选项可以解决它使用@Profile。我正在使用maven进行个人资料管理而不确定为什么我需要代码中的硬核配置文件...
有人可以帮我解决误会吗? 感谢。
答案 0 :(得分:1)
嗯,你必须以某种方式显示弹簧在特定情况下使用哪些配置。否则,如何决定创建哪个MongoDbFactory实例?所以是的,在@Configuration类之上使用@Profile。
另请注意,maven配置文件不是弹簧配置文件。可能是你没有把maven混合到那里(如果maven配置文件仅用于设置spring)。在这种情况下,您可以在运行应用时添加-Dspring.profiles.active=profile
。