具有自定义集合名称的Spring Data MongoDB存储库 [英] Spring Data MongoDB Repository with custom collection name

查看:244
本文介绍了具有自定义集合名称的Spring Data MongoDB存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data for MongoDB,我需要能够在运行时配置集合。

I am using Spring Data for MongoDB and I need to be able to configure collection at runtime.

我的存储库定义为:

@Repository
public interface EventDataRepository extends MongoRepository<EventData, String> {
}

我试过这个愚蠢的例子:

I tried this silly example:

@Document(collection = "${mongo.event.collection}")
public class EventData implements Serializable {

但是mongo.event.collection没有像@Value注释一样解析为名称。

but mongo.event.collection did not resolve to a name as it does with a @Value annotation.

多一点调试和搜索,我尝试了以下内容:
@Document(collection =#{$ {mongo.event.collection}})

A bit more debugging and searching and I tried the following: @Document(collection = "#{${mongo.event.collection}}")

这产生了一个例外:

Caused by: org.springframework.expression.spel.SpelParseException: EL1041E:(pos 1): After parsing a valid expression, there is still more data in the expression: 'lcurly({)'
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:129)
    at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:60)
    at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:32)
    at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:154)
    at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:85)

也许我只是不喜欢我不知道如何使用SPel来访问Spring的Property Configurer中的值。

Perhaps I just don't know how to quite use SPel to access values from Spring's Property Configurer.

当单步执行代码时,我发现有一种方法可以指定集合名称甚至是但是,我不确定哪个注释应该用于此目的或如何使用。

When stepping through the code, I see that there is a way to specify collection name or even expressions, however, I am not sure which annotation should be used for this purpose or how to do it.

谢谢。
-AP _

Thanks. -AP_

推荐答案

所以,最后,这是一个解决问题的方法。我想我真的不知道如何使用SPeL表达式从Spring Properties Configurer访问数据。

So, at the end, here is a work around that did the trick. I guess I really don't know how to access data from Spring Properties Configurer using the SPeL expressions.

在我的@Configuration类中:

In my @Configuration class:

@Value("${mongo.event.collection}")
private String
    mongoEventCollectionName;

@Bean
public String mongoEventCollectionName() {
    return
        mongoEventCollectionName;
}

在我的凭证上:

@Document(collection = "#{mongoEventCollectionName}")

这似乎可以正常工作并正确选取我的.properties文件中配置的名称,但是,我仍然不确定为什么我不能像在@Value注释中那样使用$来访问该值。

This, appears to work and properly pick up the name configured in my .properties file, however, I am still not sure why I could not just access the value with $ as I do in the @Value annotation.

这篇关于具有自定义集合名称的Spring Data MongoDB存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆