从 Servlet 和 JDBC 迁移到 DAO 和 Spring,有什么建议吗?
我们有带有 servlet 的 Web 应用程序,可以直接进行 JDBC 调用。我们主要用 Spring JDBC 替换了 JDBC 调用,这是一个巨大的改进(不再有泄漏连接!)。
我想进一步处理这些混乱的代码并使用 DAO。然而,我不确定如何使用混合中的 servlet 来执行此操作,因为我知道 servlet 不能 @autowired。
举个例子,现在我有一个接口:
public interface AdminDao
{
public boolean isAdmin(int id);
}
和一个实现
package myapp.dao.impl;
@Repository
public class AdminDaoSpring extends SimpleJdbcDaoSupport implements AdminDao
{
private static final String _isAdminSql
= "SELECT count(*) from admin WHERE id=?";
public boolean isAdmin(int id);
{
int cnt = getSimpleJdbcTemplate().queryForInt(_isAdminSql, id);
return (cnt > 0);
}
}
在我的 applicationContext.xml 中,我有
<bean id="adminDao" class="myapp.dao.impl.AdminDaoSpring"></bean>
一个 servlet AdminCheckServlet,它当前进行上述查询。如何更改它以使用 adminDao 实例?我无法使用 @Service 注释 servlet,因为 DAO 不会被注入,因为 servlet 是由容器(Tomcat)而不是 Spring 构建的。
我是否应该创建另一个类 AdminService,并让它使用 AdminDao 处理所有调用?然后,影响 Admin 表的 servlet 都会实例化 AdminService 并使用它,而不是直接 JDBC 调用。然而,这感觉不对。
谢谢!
保罗
We've got webapps with servlets that make JDBC calls directly. We've mostly replaced the JDBC calls with Spring JDBC which has been a vast improvement (no more leaked connections!).
I'd like to go a little farther with this mess of code and use DAOs. I'm not sure how to do this with the servlets in the mix, however, because I know the servlets can't be @autowired.
As an example, right now I've got an interface:
public interface AdminDao
{
public boolean isAdmin(int id);
}
and an implementation
package myapp.dao.impl;
@Repository
public class AdminDaoSpring extends SimpleJdbcDaoSupport implements AdminDao
{
private static final String _isAdminSql
= "SELECT count(*) from admin WHERE id=?";
public boolean isAdmin(int id);
{
int cnt = getSimpleJdbcTemplate().queryForInt(_isAdminSql, id);
return (cnt > 0);
}
}
In my applicationContext.xml I have
<bean id="adminDao" class="myapp.dao.impl.AdminDaoSpring"></bean>
I've got a servlet, AdminCheckServlet, that currently makes the above query. How do I change this to use an adminDao instance? I can't annotate the servlet with @Service because the DAO won't get injected as the servlet is constructed by the container (Tomcat) and not Spring.
Should I make another class, AdminService, and have that handle all the calls using AdminDao? The servlets affecting the Admin table would all then instantiate AdminService and use that instead of direct JDBC calls. That doesn't feel right, however.
Thanks!
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会研究 SpringMVC,并使用 Spring 控制器而不是直接使用 java servlet。
Spring MVC
使用起来非常简单。您创建一个简单的 web.xml 部署描述符以使端点调用 Springs DispatcherServlet。完成此操作后,您可以创建一个控制器类以将这些端点映射到控制器中的方法。您的控制器可以定义为 applicationContext 的一部分,因此可以注入其 DAO(或其他服务)。
I would look into SpringMVC, and use a Spring Controller instead of using java servlets directrly.
Spring MVC
It is pretty easy to use. You create a simple web.xml deployment descriptor to have your endpoints call Springs DispatcherServlet. With this done, you can create a controller class to map these endpoints to methods in the controller. Your controller can be defined as a part of your applicationContext, and can therefore have its DAO (or other services) injected.
您需要使用 MVC 框架(最流行的是 Struts 1.x、Struts 2 和 Spring MVC),并且您将能够从控制器(在 Struts 框架中称为“操作”)调用 daos。
这是关于此的宝贵资源: http://www.ibm .com/developerworks/java/library/j-sr2/index.html
如果您没有太多可重用的业务逻辑,我不确定您是否需要服务。
You need to use a MVC Framework (the most popular are Struts 1.x, Struts 2 and Spring MVC), and you will be able to call you daos from your controllers (which are called "Actions" in Struts frameworks).
Here is a valuable resource about this : http://www.ibm.com/developerworks/java/library/j-sr2/index.html
I'm not sure you need services, if you don't have much reusable business logic.