[英]How to dynamically manage multiple datasources
Similar topics have been covered in other threads, but I couldn't find a definitive solution to my problem.其他主题中已经涵盖了类似的主题,但我找不到解决问题的明确方法。
What we're trying to achieve is to design a web app which is able to:我们想要实现的是设计一个 web 应用程序,它能够:
At the moment we have a DAO Manager object which is able to read the datasource configuration file and instantiate multiple session factories, saving them in a map.目前我们有一个 DAO 管理器 object,它能够读取数据源配置文件并实例化多个 session 工厂,将它们保存在 map 中。 Each session factory is created with a configuration containing the proper hibernate mapping classes (different for each database schema).
每个 session 工厂都是使用包含正确 hibernate 映射类(每个数据库模式不同)的配置创建的。 Moreover we have multiple DAO interfaces with their implementations, used to access "their database".
此外,我们有多个 DAO 接口及其实现,用于访问“他们的数据库”。
At this point we would need a way to get from the DAO Manager a specific DAO object, containing the right session factory attached, all based on the user request (basically a call from the above service containing the datasource id or a custom datasource object).此时,我们需要一种方法从 DAO 管理器获取特定的 DAO object,其中包含附加的正确 session 工厂,所有这些都基于用户请求(基本上是来自上述服务的调用,包含数据源 ID 或自定义数据源对象) .
Ideally the service layer should use the DAO Manager to get a DAO object based on the datasource id (for instance), without worrying about it's actual implementation: the DAO Manager would take care of it, by creating the correct DAO object and injecting in it the right session factory, based on the datasource id.理想情况下,服务层应该使用 DAO 管理器来获取基于数据源 ID(例如)的 DAO object,而不用担心它的实际实现:DAO 管理器会通过创建正确的 DAO object 并注入它来处理它正确的 session 工厂,基于数据源 id。
My questions are:我的问题是:
Is this a good approach to follow?
这是一个很好的方法吗?
It's probably the only possible approach.这可能是唯一可能的方法。 So, yes.
所以,是的。
How can I use Spring to dynamically inject in the DAO Manager multiple DAO implementations for each DAO interface?
如何使用 Spring 在 DAO 管理器中为每个 DAO 接口动态注入多个 DAO 实现?
Dynamically?动态的? I thought you wanted to do it at startup time.
我以为你想在启动时这样做。 If so, just provide an accessor with a list or array:
如果是这样,只需提供一个带有列表或数组的访问器:
public void setMyDaos(List<Mydao> daos){
this.daos = daos;
}
Once the session factories are created, is there a way to let Spring handle them, as I would normally do with dependency injection inside the application-context.xml?
一旦创建了 session 工厂,有没有办法让 Spring 处理它们,就像我通常在 application-context.xml 中进行依赖注入一样?
This one's tough.这个好难I'd say you will probably have to store your sessionFactory bean in
scope=session
我会说你可能不得不将你的 sessionFactory bean 存储在
scope=session
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.