使用@Document进行mongodb多语言拼写 [英] mongodb multi tenacy spel with @Document
问题描述
这与@ bocument中的
MongoDB和SpEL表达式有关注释
This is related to MongoDB and SpEL Expressions in @Document annotations
这是我创建mongo模板的方式
This is the way I am creating my mongo template
@Bean
public MongoDbFactory mongoDbFactory() throws UnknownHostException {
String dbname = getCustid();
return new SimpleMongoDbFactory(new MongoClient("localhost"), "mydb");
}
@Bean
MongoTemplate mongoTemplate() throws UnknownHostException {
MappingMongoConverter converter =
new MappingMongoConverter(mongoDbFactory(), new MongoMappingContext());
return new MongoTemplate(mongoDbFactory(), converter);
}
我有租户提供商类
@Component("tenantProvider")
public class TenantProvider {
public String getTenantId() {
--custome Thread local logic for getting a name
}
}
我的域类
@Document(collection = "#{@tenantProvider.getTenantId()}_device")
public class Device {
-- my fields here
}
如你所见我创造了我的mongotemplate在帖子中指定,但我仍然得到以下错误
As you see I have created my mongotemplate as specified in the post, but I still get the below error
线程mainorg.springframework.expression.spel中的异常。 SpelEvaluationException:EL1057E:(pos 1):没有在上下文中注册的bean解析器来解析对bean'tenantProvider'的访问
Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'tenantProvider'
我是什么做错了吗?
推荐答案
最后图为什么我会遇到这个问题。
Finally figured out why i was getting this issue.
使用Servlet 3初始化时,请确保将应用程序上下文添加到mongo上下文中,如下所示
When using Servlet 3 initialization make sure that you add the application context to the mongo context as follows
@Autowired
private ApplicationContext appContext;
public MongoDbFactory mongoDbFactory() throws UnknownHostException {
return new SimpleMongoDbFactory(new MongoClient("localhost"), "apollo-mongodb");
}
@Bean
MongoTemplate mongoTemplate() throws UnknownHostException {
final MongoDbFactory factory = mongoDbFactory();
final MongoMappingContext mongoMappingContext = new MongoMappingContext();
mongoMappingContext.setApplicationContext(appContext);
// Learned from web, prevents Spring from including the _class attribute
final MappingMongoConverter converter = new MappingMongoConverter(factory, mongoMappingContext);
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return new MongoTemplate(factory, converter);
}
检查上下文的自动装配以及
mongoMappingContext .setApplicationContext(appContext);
Check the autowiring of the context and also mongoMappingContext.setApplicationContext(appContext);
通过这两行,我能够正确连接组件,以便在多租户模式下使用它
With these two lines i was able to get the component wired correctly to use it in multi tenant mode
这篇关于使用@Document进行mongodb多语言拼写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!