使用 Spring 实现 DAO
我正在考虑通过依赖注入实现 Objectify DAO,这样我就可以维护我的代码来访问相同的“Dao”,而将来实现可能会从 Objectify 更改为 Hibernate-MySQL 或 MongoDb,而无需担心更改中的任何代码UI 或客户端。
UserDao 基于此处的示例: http://turbomanage.wordpress.com/2010/01/28/ Simply-with-objectify/
UserObjectifyDaoImpl implements Dao<User> {
private UserDao dao = null;
public void put(User entity) {
if (dao == null) {
dao = new UserDao();
}
dao.put(entity);
}
// other put and set methods
}
这样,我就有了 context.xml
:
<bean id="userDao" class="com.example.server.daoimpl.UserObjectifyDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
如果我需要更改实现,我只需要将这个 bean 更改为UserObjectifyDaoImpl
类似于:
UserHibernateDaoImpl
或 UserMongoDBDaoImpl
或保存到任何数据库的任何实现。
并且 UI/客户端中的代码仍然完好无损,例如:
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Dao dao = (Dao) ctx.getBean("userDao");
dao.put(something);
我现在需要执行此操作的原因之一是,我需要使用应用程序引擎(通过 objectify)进行开发,但是将来我可能需要将一些数据访问对象更改为hibernate 和一些 mongodb (所以它是一个混合)。
我还没有测试过这段代码,这个策略有效吗?
I'm thinking of implementing Objectify DAO with dependency injection, such that I can maintain my code to access the same "Dao" while the implementation may change from Objectify to Hibernate-MySQL or MongoDb in the future without me worrying on changing any code in the UI or client side.
UserDao is based on the example here:
http://turbomanage.wordpress.com/2010/01/28/simplify-with-objectify/
UserObjectifyDaoImpl implements Dao<User> {
private UserDao dao = null;
public void put(User entity) {
if (dao == null) {
dao = new UserDao();
}
dao.put(entity);
}
// other put and set methods
}
Such that, I have the context.xml
:
<bean id="userDao" class="com.example.server.daoimpl.UserObjectifyDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
And if I need to change the implementation, I just need to change this bean from UserObjectifyDaoImpl
to something like:
UserHibernateDaoImpl
or UserMongoDBDaoImpl
or whatever implementation saving to whatever database.
And still have my code in the UI / Client intact, like:
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Dao dao = (Dao) ctx.getBean("userDao");
dao.put(something);
One reason I need to do this right now, I need to develop using app engine (via objectify), however in the future I may need to change some data access objects to hibernate and some to mongodb (so its a mix).
I haven't tested this code, will this strategy work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这会起作用。事实上,这是发明 DI 和接口编码的主要原因之一。只需确保所有 DAO 实现都遵循相同的契约(DAO 经常引入泄漏抽象)。
此外,您还有其他几个选项来实现相同的目标:
几个
@Service
带注释的类,其中一个标记为@Primary
(如果您使用自动装配)Spring 配置文件 和选择性激活 beans
顺便说一句,如果您正在考虑切换到不同的 DAO 实现,请查看
CrudRepository
来自 Spring 数据。 Spring Data 项目提供了几个为 MongoDB、Neo4J、JPA 等实现此接口的模块。目前看来,几个 Spring Data 模块不能很好地协同工作(请参阅:DATAJPA-146),因此,如果您选择实现
CrudRepository
,请确保此问题已解决,否则您可以解决它。感谢 @iddqd 指出这一点。Yes, this will work. In fact this is one of the major reasons why DI and coding to an interface was invented. Just make sure that all DAO implementations follow the same contract (DAOs very often introduce leaky abstractions).
Also you have several other options to achieve the same goal:
Several
@Service
annotated classes with one marked as@Primary
(if you are using autowiring)Spring profiles and selective activation of beans
BTW if you are considering switching to a different DAO implementation, have a look at
CrudRepository
from Spring Data. Spring Data project provides several modules that implement this interface for MongoDB, Neo4J, JPA, etc.For the time being it seems like several Spring Data modules do not play together nicely (see: DATAJPA-146), so if you choose to implement
CrudRepository
make sure this issue is fixed or you can work it around. Thanks to @iddqd for pointing that out.如果您在应用程序中只需要一种实现,则可以将上下文配置更改为选定的 Dao 实现,但如果您在应用程序中需要多个实现(混合模式),则需要设计工厂层。您尝试设计一个名为 Factory 的层及其 API 和实现,它决定在任何时候必须选择女巫 Dao(Hibernate、MongoDB、JP 或等)。
You can changes context config to selected Dao implementation if you only need one implementation in application but if you need more than one implementation in your application(mixing mode), you need design Factory Layer. You trying design a layer with name Factory and with its APIs and implementations and it decide witch Dao(Hibernate, MongoDB, JP or etc) in any time must select.