我将使用Spring Data for MongoDB,并需要在运行时配置集合。
我的存储库定义如下:
但是mongo.event.collection没有像@Value注释一样解析为名称。
稍作调试和搜索之后,我尝试了以下代码: @Document(collection = "#{${mongo.event.collection}}")
这将产生一个异常:
也许我只是不知道如何使用SPel从Spring的属性配置器中访问值。
当我在代码中单步执行时,我看到有一种指定集合名称甚至表达式的方式,但我不确定应该使用哪个注释来实现此目的或如何做到这一点。
谢谢。 -AP_
我的存储库定义如下:
@Repository
public interface EventDataRepository extends MongoRepository<EventData, String> {
}
我尝试了这个愚蠢的例子:
@Document(collection = "${mongo.event.collection}")
public class EventData implements Serializable {
但是mongo.event.collection没有像@Value注释一样解析为名称。
稍作调试和搜索之后,我尝试了以下代码: @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的属性配置器中访问值。
当我在代码中单步执行时,我看到有一种指定集合名称甚至表达式的方式,但我不确定应该使用哪个注释来实现此目的或如何做到这一点。
谢谢。 -AP_
...
你正在使用哪个版本的spring-data-mongodb? - Benjamin M