@Document注释中的MongoDB和SpEL表达式 [英] MongoDB and SpEL Expressions in @Document annotations

查看:579
本文介绍了@Document注释中的MongoDB和SpEL表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SpEL根据我定义的一些规则将相同的Document加载到不同的集合中。

I am trying to use the SpEL to load the same Document into different collections based on some rules that i have defined.

所以从我拥有的开始:

So to start with what i have:

- 首先是所有文件:

-first of all the document:

@Document(collection = "#{@mySpecialProvider.getTargetCollectionName()}")
public class MongoDocument {
// some random fields go in
}

-second我有应该提供集合名称的提供者bean:

-second i have the provider bean that should provide the collection name:

@Component("mySpecialProvider")
public class MySpecialProvider {

public String getTargetCollectionName() {
// Thread local magic goes in bellow
    String targetCollectionName = (String) RequestLocalContext.getFromLocalContext("targetCollectionName");
    if (targetCollectionName == null) {
        targetCollectionName = "defaultCollection";
    }
    return targetCollectionName;
 }
}

问题是当我尝试插入文档时到应该由提供者生成的特定集合中我得到以下stacktrace:

The problem is that when i try to insert a document into a specific collection that should be generated by the provider i get the following stacktrace:

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

我也试过使spring组件 ApplicationContextAware 但仍然没有运气。

I also tried making the spring component ApplicationContextAware but still no luck.

推荐答案

正如我所承诺的那样,我回答了我的问题。
要使其正常工作,您需要在应用程序上下文XML文件中对 mongoTemplate bean进行以下设置:

As i promised i am returning with an answer to my question. To have it working you need to have the following settings for the mongoTemplate bean in the application context XML file:

<mongo:db-factory dbname="${myDatabaseName.from.properties.file}" mongo-ref="mongo"/>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>   
<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter" c:mongoDbFactory-ref="mongoDbFactory"
            c:mappingContext-ref="mongoMappingContext"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
            c:mongoDbFactory-ref="mongoDbFactory" c:mongoConverter-ref="mappingMongoConverter"/>

使用上面的设置和我在问题中建议的解决方案。您可以使用相同的域对象,并根据您选择的设置将其存储到多个集合中。

And with the settings above and the solution i suggested in my question works. You can use the same domain object and store it into multiple collections based on settings of your choosing.

编辑:

由于有人在相关问题中提出要求,我还将更新ThreadLocal上下文的逻辑:

Since someone asked for it in a related question, i will also update here the logic for the ThreadLocal context:

创建一个包含以下实现的 RequestLocalContext 类:

Make a RequestLocalContext class that wraps over the following implementation:

private static final ThreadLocal<Map> CONTEXT = new ThreadLocal<Map>() {
        protected Map initialValue() {
            Map localMap = new HashMap();
            localMap.put(LocalContextKeys.CONVERSATION_CONTEXT, new HashMap());
            return localMap;
        };
    };

public static void putInLocalContext(Object key, Object value) {
    Map localMap = CONTEXT.get();
    localMap.put(key, value);
}

 public static Object getFromLocalContext(Object key) {
    Map localMap = CONTEXT.get();
    return localMap.get(key);
}

其中 LocalContextKeys 是包含允许的键的枚举在 ThreadLocal 上下文地图中。请注意,这些键是用户定义的,因此您可以根据需要随意添加。

Where LocalContextKeys is an enum containing the keys allowed in the the ThreadLocal context Map. Note that the keys are user defined so feel free to put in there whatever you may need.

这篇关于@Document注释中的MongoDB和SpEL表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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