锁定老帖子 主题:spring最佳实践中的这一段代码
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-03-11
明白了. 我看了spring中 sample的petStore的代码,发现他的DAO层方法都抛出的是 DataAccessException,而DataAccessException是个RuntimeException,因此他在struts的Action中(或者一个普通的 Servlet中)就不用捕获这个异常. 但是如果DAO层都统一的抛出了DataAccessException,我怎样针对某种特殊的情况进行某种特殊的处理呢? 我得意思就是说如果我用以前的方法:
在DAO中抛出了具体的自定义Exception,比如Myexception和 YourException, 我在 servlet中就可以 ................................... catch(myexception myex);{ mapping.findforward("myerror.jsp");; }catch(yourexception yourex);{ mapping.findforward("yourerror.jsp");; } 从而定向到某一个具体错误页面, 但是现在如果用spring的方法,DAO层都统一抛出DataAccessException这个 RuntimeException,而我在Servlet中又不用捕获他(因为DataAccessException是 UnChecked的),那麽怎样达到上面那个定位到具体错误页面的要求呢? |
|
返回顶楼 | |
发表时间:2005-03-11
除非我自定义一些继承自DataAccessException的异常类,然后在我的Action中 catch住他们进行特殊的处理,就可以做到以前的效果
|
|
返回顶楼 | |
发表时间:2005-03-11
Spring的DAO层方法都抛出的DataAccessException,并不表示该方法抛出DataAccessException这种类型的异常,它可以是DataAccessException,也可以是DataAccessException的子类异常,你可以在servlet捕获子类异常。这些异常正如robbin说的
robbin 写道 Spring的DAOException内部有一个数据库出错代码和DAOException继承类树的对照表,可以把JDBC Driver抛出的、Java应用代码无法直接处理的数据库出错代码字符串转化为Spring的相应类型的DAO异常,这样就可以让你针对相应的数据库异常进行相应的异常处理了。
|
|
返回顶楼 | |
发表时间:2005-03-11
找不到DAOException这个类,是不是DataAccessException呢? 我想看看那个继承树的对照表的实现,不过好像在里面没有啊。
robbin 写道 这个问题问得很好。Spring的DAOException有几个重要的作用:
1、传统的JTA 异常,或者说应用服务事务处理异常都没有一个共同的父类,这样你必须写一堆catch来处理,非常臃肿不堪。而Spring的DAOException有共同的父类,可以解决这个问题。 2、传统的JTA异常或者说应用服务事务处理异常是CheckedException的,你明知无法处理,还不得不捕获,更糟糕的是有人捕获以后不做任何处理,而Spring的DAOException转化为UncheckedException让有能力的处理的程序去处理。 3、Spring的DAOException内部有一个数据库出错代码和DAOException继承类树的对照表,可以把JDBC Driver抛出的、Java应用代码无法直接处理的数据库出错代码字符串转化为Spring的相应类型的DAO异常,这样就可以让你针对相应的数据库异常进行相应的异常处理了。 |
|
返回顶楼 | |
发表时间:2005-03-11
The reason why change checked exception to unchecked exception are listed as follows:
1. client does not have ability to recover checked exception. 2. Throwing checked exception to upper level such as businees tier destroy preservation encapsulation. 3. checked exception force client throw checked exception although we don't need. Weird! As robbin said, DataAccessException is a parent. We throw this runtimeException to client. We still can use if (ex instanceof customizedException) on the client side such as Action or JSP. That means, we still can tell different exception on client side |
|
返回顶楼 | |
发表时间:2005-03-13
bruce 写道 The reason why change checked exception to unchecked exception are listed as follows:
1. client does not have ability to recover checked exception. 2. Throwing checked exception to upper level such as businees tier destroy preservation encapsulation. 3. checked exception force client throw checked exception although we don't need. Weird! As robbin said, DataAccessException is a parent. We throw this runtimeException to client. We still can use if (ex instanceof customizedException) on the client side such as Action or JSP. That means, we still can tell different exception on client side 你的意思是这样: 写一个继承DataAccessException的类. For example: public class MyDataAccessException extends DataAccessException{ ................................. } public class YourDataAccessException extends DataAccessException{ ................................. } In servlet , 这样写:: public class myServlet extends HttpServlet{ public void doPost(.......................);....{ try{ mydao.getList();; }catch(MyDataAccessException myex);{ //do something }catch(YourDataAccessException yourex);{ //do something } |
|
返回顶楼 | |
发表时间:2005-03-13
spring aop声明事务里可以设置对某一exception进行rollback的
|
|
返回顶楼 | |
发表时间:2005-03-15
引用 写一个继承DataAccessException的类. For example:
java代码: public class MyDataAccessException extends DataAccessException{ ................................. } public class YourDataAccessException extends DataAccessException{ ................................. } In servlet , 这样写: java代码: : public class myServlet extends HttpServlet{ public void doPost(.......................)....{ try{ mydao.getList(); }catch(MyDataAccessException myex){ //do something }catch(YourDataAccessException yourex){ //do something } Or in this way: java代码: public void doPost(.......................)....{ try{ mydao.getList(); }catch(Exception myex){ if(myex instanceof MyDataAccessException){ //do something } }catch(YourDataAccessException yourex){ if(yourex instanceof YourDataAccessException){ //do something } } 是这样吧 应该是第二种了。 引用 写一个继承DataAccessException的类. For example:
java代码: public class MyDataAccessException extends DataAccessException{ ................................. } public class YourDataAccessException extends DataAccessException{ ................................. } 可是继承的不是DataAccessException,而是你所要处理的异常类。 |
|
返回顶楼 | |