【问题标题】:Clarification about @EnableMongoRepositories关于@EnableMongoRepositories 的说明
【发布时间】:2014-10-16 10:51:21
【问题描述】:

我正在尝试使用 @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 进行配置文件管理,但不知道为什么我需要代码中的核心配置文件...

谁能帮我解决误解? 谢谢。

【问题讨论】:

    标签: spring spring-data maven-profiles spring-profiles


    【解决方案1】:

    好吧,您必须以某种方式向 spring 展示哪些配置用于特定情况。否则怎么可能决定创建哪个 MongoDbFactory 实例?所以是的,在两个 @Configuration 类之上使用 @Profile。

    另外请注意,maven 配置文件不是弹簧配置文件。可能是您不必将 maven 混入其中(如果 maven 配置文件仅用于设置弹簧之一)。在这种情况下,您可以在运行应用程序时添加-Dspring.profiles.active=profile

    【讨论】:

    • 据我了解,params 应该有助于决定应该使用什么配置:mongoTemplateRef 和 basePackages。
    • 你想同时使用两个数据库吗?
    • 如果是这样 - 我不会因为那个异常而责怪 EnableMongoRepositories。谷歌搜索似乎导致 Environment 不能为空!可能是混合依赖版本的结果。
    • 是的,我想要两个 daos 的两个数据源。 Datasource1 用于 dao1,datasource2 用于 dao2。为什么我需要为此配置配置文件?..
    • 你没有,我接受了你的建议(提到配置文件),因为你必须在某些情况下打开它们(比如不同的环境/语言环境)。如上所述-检查您的春季版本或谷歌异常-它没有说明混合存储库配置
    猜你喜欢
    • 2019-08-29
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多