日期:2014-05-19  浏览次数:20728 次

log4j的使用
struts2+hibernate+spring 做架构 使用log4j做日志。
在记录错误日志时,应该怎么处理 不会每次执行 hql语句的时候都要 try catch 一下吧 这样做是不是太麻烦了
求高手赐教

------解决方案--------------------
冒似可以用spring的aop,弄个切面处理日志,然后在处理正常逻辑。
------解决方案--------------------
spring的AOP可以
------解决方案--------------------
既然是spring的,只要是使用了spring的事务管理,那就不需要自己try catch
2种方案
1.在xml文件中配置一个exceptionResolver,可以自定义扑捉哪种Exception,以及重定向的路径
[code=xml]
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="failure" />
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">exception </prop>
</props>
</property>
</bean>
[/code]

2.以注解方式的做法
实现一个继承AbstractHandlerExceptionResolver的类,并且将其纳入spring的bean管理
类似这样:
Java code

@Controller
public class ExceptionResolver extends AbstractHandlerExceptionResolver{

    private Log log = LogFactory.getLog(ExceptionResolver.class);
    
    @Override
    protected ModelAndView doResolveException(
            HttpServletRequest httpservletrequest,
            HttpServletResponse httpservletresponse, Object obj,
            Exception exception) {
        ModelAndView mv = new ModelAndView("exception");
        log.debug(exception.getMessage(),exception);

        mv.addObject("exception", exception);
        
        return mv;
    }

}