【问题标题】:Cannot resolve bean in SpEL for Spring Data MongoDB collection name无法在 SpEL 中为 Spring Data MongoDB 集合名称解析 bean
【发布时间】:2017-06-01 14:14:37
【问题描述】:

我正在尝试使用 Spring Data MongoDB 和 Spring Batch 自定义实体类保存和索引的集合名称。该类声明如下:

@Document
@CompoundIndex(name = "unique_source", def = "{'fid': 1, 'sid': 1}", unique = true, background = true)
public class VariantSource {
    ...
}

还有项目作者:

public class VariantSourceMongoWriter extends MongoItemWriter<VariantSource> {

    public VariantSourceEntityMongoWriter(MongoOperations mongoOperations, String collectionName) {
        setTemplate(mongoOperations);
        setCollection(collectionName);
    }
}

保存工作正常:对象被写入作为参数提供的集合中。问题是索引是在默认集合中创建的,以类名 (variantSource) 命名。

阅读thisthis后,我创建了以下内容:

public class MongoCollections {

    public String getCollectionFilesName() {
        return "my_custom_collection_name"; // TODO Dynamic value
    }
}


@Configuration
public class MongoCollectionsConfiguration {

    @Bean
    public MongoCollections mongoCollections() {
        return new MongoCollections(); 
    }


@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {MongoCollectionsConfiguration.class})
public class VariantSourceMongoWriterTest {

    @Autowired
    private MongoCollections mongoCollections;

}

我已检查该实例是否正确自动装配到单元测试中,但我无法使其与 SpEL 一起使用。

@Document 注释更改为如下所示:

@Document(collection = "#{@mongoCollections.getCollectionFilesName()}")

抛出以下异常:

org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): 没有在上下文中注册 bean 解析器来解析对 bean 'mongoCollections' 的访问

如果我使用这个:

@Document(collection = "#{mongoCollections.getCollectionFilesName()}")

这个例外:

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): 在 null 上找不到属性或字段 'mongoCollections'

最后,下面创建了一个指定名称的集合,包括符号:

@Document(collection = "@mongoCollections.getCollectionFilesName()")

【问题讨论】:

  • 感谢使用@mongoCollections.getCollectionFilesName() 的提示,这对我也有用!有谁知道为什么会这样?这甚至是有效的 SpEL 吗?
  • @JanGassen @mongoCollections.getCollectionFilesName() 对我不起作用,它最终创建了一个名为 @mongoCollections.getCollectionFilesName() 的集合(非动态)。我正在使用 Spring Data 2.2.10。

标签: spring-boot spring-batch spring-data-mongodb spring-el


【解决方案1】:

只需更改我的MongoTemplate 配置文件,我就可以让我的@Document 标记访问一个bean。

以前,我是这样设置的:

@Configuration
public class MongoTemplateConfiguration {

    ...

    @Bean
    public MongoTemplate mongoTemplate() {
        ...
        return new MongoTemplate(...);
    }
}

将其更改为遵循this (3.2 Java Configuration) 格式是我消除“bean 解析器”错误所需的全部内容:

@Configuration
public class MongoTemplateConfiguration extends AbstractMongoClientConfiguration {

    ...

    @Override
    @Bean
    public com.mongodb.client.MongoClient mongoClient() {
        MongoClientSettings settings = ...;
        return MongoClients.create(settings);
    }
}

【讨论】:

    【解决方案2】:

    确保您的 bean mongoCollections 已在应用程序上下文中注册, 并更正SpEL表达式,如下所示。

    @Document(collection = "#{@mongoCollections.getCollectionFilesName()}")

    【讨论】:

      【解决方案3】:

      正如this answer 所指出的,修复注入:

      @Document(collection = "#{mongoCollections.getCollectionFilesName()}") 
      

      SpelEvaluationException: EL1007E:(pos 0): 在 null 上找不到属性或字段“mongoCollections”

      (或直接方法bean:@Document(collection = "#{getCollectionFilesName}")),尝试将ApplicationContext设置为MongoMappingContext(用于实例化MongoConverter,后来是MongoTemplate):

      @Bean
      public MongoMappingContext MongoMappingContext() {
          MongoMappingContext mappingContext = new MongoMappingContext();
          mappingContext.setApplicationContext(applicationContext);
          return mappingContext;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-08-29
        • 2016-05-30
        • 2020-03-21
        • 1970-01-01
        • 1970-01-01
        • 2022-11-13
        • 2017-08-01
        • 2015-06-18
        • 2020-12-11
        相关资源
        最近更新 更多