Spring3.0中的错误处理配方法

Spring3.0中的异常处理配方法
    spring3.0中对异常的处理方法一共提供了两种:一种是使用<span style="color: #0000ff;">handlerexceptionresolver</span>接口;一种是在controller类内部使用<span style="color: #0000ff;">@exceptionhandler</span>注解。使用第一种方式可以实现全局异常控制,并且spring已经提供了一个默认的实现类simplemappingexceptionresolver;使用第二种方式可以在controller内部实现更个性化点异常处理方式,灵活性更高。一般来说,项目中只需要采用第一种方式就可以了,每个人都自己定义异常的展现方式,太过个性了,不统一。
 
从目前的调查结果看,这两种方式不能共存,不知道未来的版本是否能将他们合二为一,这样才能灵活配置。
<h3>基于handlerexceptionresolver接口的异常处理:</h3>使用这种方式只需要实现resolveexception方法,该方法返回一个modelandview对象,在方法内部对异常的类型进行判断,然后常见合适的modelandview对象,如果该方法返回了null,则spring会继续寻找其他的实现了handlerexceptionresolver接口的bean。换句话说,spring会搜索所有注册在其环境中的实现了handlerexceptionresolver接口的bean,逐个执行,直到返回了一个modelandview对象。
示例代码:
/** * 基于handlerexceptionresolver接口的异常处理类 * @author zywang 2011-4-2 */public class customexceptionhandler implements handlerexceptionresolver {	@override	public modelandview resolveexception(httpservletrequest request,			httpservletresponse response, object object, exception exception) {		if(exception instanceof ioexception){			return new modelandview("ioexp");		}else if(exception instanceof sqlexception){			return new modelandview("sqlexp");		}		return null;	}}
这个类必须声明到spring中去,让spring管理它,你可以使用@component标签,也可以使用<bean/>节点。为了简单的进行异常处理,spring提供了simplemappingexceptionresolver类,该类实现了handlerexceptionresolver接口,需要使用时只需要使用<bean/>节点进行声明即可,示例如下:
	<bean class="org.springframework.web.servlet.handler.simplemappingexceptionresolver">		<!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->		<property name="defaulterrorview" value="error"></property>		<!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->		<property name="exceptionattribute" value="ex"></property>		<!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->		<property name="exceptionmappings">			<props>				<prop key="ioexception">error/ioexp</prop>				<prop key="java.sql.sqlexception">error/sqlexp</prop>			</props>		</property>	</bean>
<h3>基于@exceptionhandler的异常处理:</h3>该方法需要定义在controller内部,然后创建一个方法并用@exceptionhandler注解来修饰用来处理异常,这个方法基本和@requestmapping修饰的方法差不多,只是可以多一个类型为exception的参数,@exceptionhandler中可以添加一个或多个异常的类型,如果为空的话则认为可以触发所有的异常类型错误。
示例代码:
/** * 基于@exceptionhandler异常处理 * @author zywang 2011-4-2 */@controllerpublic class exceptionhandlercontroller {		@exceptionhandler(value={ioexception.class,sqlexception.class})	public string exp(exception ex,httpservletrequest request) {		request.setattribute("ex", ex);		return "/error.jsp";	}}
 
以上配置基于spring3.05进行设置,参考其《reference documentation》