1. 程式人生 > >在java Spring基礎上實現自定義異常處理框架教程

在java Spring基礎上實現自定義異常處理框架教程

應用專案大致的體系結構:

    
 

 該異常處理框架滿足的要求:

  • 完整的異常組織結構
  • 異常的統一處理
  • 可配置,受管式,方便使用

完整的異常組織結構:

  • 使用者可以方便的定義自己的異常,但所有UncheckedException需要繼承BaseAppRuntimeException,所有的checked Exception可以繼承BaseAppException,或者需要丟擲且不需要check時用WrapperredAppException封裝後丟擲
  • 合理地使用checked異常
  • Exception有唯一的error code,這樣使用者報告異常後,可以根據異常號找到相應Exception,把exception直接顯示給使用者也沒有太大的意義,如何紀錄exception那就是下文講到的ExceptionHandler的職責了。
  • 如果是第三方包括jdk中的異常,需要封裝成BaseAppException或者BaseAppRuntimeException後丟擲

                                     

統一的異常處理

異常統一在框架中進行處理,不需要在上層應用的程式碼中去處理丟擲的異常。為了儘量捕捉到所有的異常,將異常處理放在了ActionBroker中,這樣凡是action以後丟擲的異常都可以捕捉到,因為webservice只是簡單的呼叫action類的方法,一般不會出現異常。當我們捕捉到異常後,需要進行異常處理,定義了ExceptionHandler介面,用介面抽象出異常處理類的具體實現。


                        

USFContextFactory: 建立ExceptionContext的工廠

package com.ldd0.exception.context;

public class CoreContextFactory {
    private static CoreContextFactory instance;

    private volatile ExceptionContext exceptionContext;

    private Object exceptionContextLock = new Object();

    private CoreContextFactory() {

    }

    public static synchronized CoreContextFactory getInstance() {
        if (null == instance) {
            instance = new CoreContextFactory();
        }
        return instance;
    }

    public ExceptionContext getExceptionContext() {
        ExceptionContext tempExpContext = exceptionContext;
        if (tempExpContext == null) { 
            synchronized (exceptionContextLock) {
                tempExpContext = exceptionContext;
                if (tempExpContext == null)
                    exceptionContext = tempExpContext = new ExceptionContext();
            }
        }
        return tempExpContext;
    }
}

ExceptionContext: 存放全域性的exception資訊

package com.ldd600.exception.context;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.Assert;

import com.ldd600.exception.base.BaseAppRuntimeException;
import com.ldd600.exception.base.ConfigException;
import com.ldd600.exception.base.handler.ExceptionHandler;
import com.ldd600.exception.config.ExceptionDefinition;

public class ExceptionContext {
    private Map<Class<?>, ExceptionDefinition> exceptionMap;

    private Map<String, ExceptionHandler> handlers = new HashMap<String, ExceptionHandler>();

    ExceptionContext() {
        exceptionMap = new HashMap<Class<?>, ExceptionDefinition>();
    }

    public boolean containsException(Class<?> expClazz) {
        return (exceptionMap.containsKey(expClazz));
    }
    
    public void addExceptionHander(Class<?> expClazz, Class<? extends ExceptionHandler> handlerClazz) {
        try {
            ExceptionDefinition definition = getRealExceptionDefinition(expClazz);
            if (null == definition) {
                throw new IllegalArgumentException(expClazz.getName() + "not in the context, please configure or add it to the context first!!");
            } 
            ExceptionHandler handler = handlers.get(handlerClazz.getName());
            if (null == handler) {
                handler = handlerClazz.newInstance();
                handlers.put(handlerClazz.getName(), handler);
            }
            
            definition.getHandlerNames().add(handlerClazz.getName());
        } catch (Exception ex) {
            throw new ConfigException("Add exception handler to context failure!", ex);
        }
    }
    
    public void addExceptionHandler(Class<?> expClazz, String errorCode, Class<? extends ExceptionHandler> handlerClazz) {
        Assert.hasLength(errorCode, expClazz + " errorCode must not be null or empty string!");
        ExceptionDefinition definition = getRealExceptionDefinition(expClazz);
        if(null == definition) {
            definition = new ExceptionDefinition(errorCode);
            exceptionMap.put(expClazz, definition);
        }
        addExceptionHander(expClazz, handlerClazz);
    }
    
    
    
    public void addExceptionHandlers(Class<?> expClazz, Class<? extends ExceptionHandler> handlerClazzes) {
        for(Class<? extends ExceptionHandler> handlerClazz : handlerClazzes) {
            addExceptionHander(expClazz, handlerClazz);
        }
    }

    public void removeExceptionHandler(Class<?> expClazz, Class<? extends ExceptionHandler> handlerClazz) {
        Assert.isTrue(containsException(expClazz));
        String handlerName = handlerClazz.getName();
        getExceptionDefinition(expClazz).getHandlerNames().remove(handlerName);
        Collection<ExceptionDefinition> definitons = exceptionMap.values();
        boolean isClearHandler = true;
        for (ExceptionDefinition expDefinition : definitons) {
            if (expDefinition.getHandlerNames().contains(handlerName)) {
                isClearHandler = false;
                break;
            }
        }

        if (isClearHandler) {
            handlers.remove(handlers.get(handlerName));
        }
    }

    public void setExceptionDefinition(Class<?> expClazz, ExceptionDefinition definition) {
        exceptionMap.put(expClazz, definition);
    }

    public ExceptionDefinition getExceptionDefinition(Class<?> expClazz) {
        if (containsException(expClazz)) {
            return exceptionMap.get(expClazz);  
        } else if (BaseAppRuntimeException.class.isAssignableFrom(expClazz.getSuperclass())) {
            return getExceptionDefinition(expClazz.getSuperclass());
        } else {
            return null;
        }
    }
    
    public ExceptionDefinition getRealExceptionDefinition(Class<?> expClazz) {
        return exceptionMap.get(expClazz);
    }

    public List<ExceptionHandler> getExceptionHandlers(Class<?> expClazz){
        ExceptionDefinition definition = getExceptionDefinition(expClazz);
        if (null != definition) {
            Set<String> handlerNames = definition.getHandlerNames();
            List<ExceptionHandler> handlerList = new ArrayList<ExceptionHandler>(handlerNames.size());
            for (String handlerName : handlerNames) {
                ExceptionHandler handler = handlers.get(handlerName);
                handlerList.add(handler);
            }
            List<ExceptionHandler> resultHandlerList = new ArrayList<ExceptionHandler>(handlerList);
            return resultHandlerList;
        } else {
            return Collections.<ExceptionHandler> emptyList();
        }
    }
    
    public String getErrorCode(Class<?> expClazz){
        ExceptionDefinition definition = getExceptionDefinition(expClazz);
        if (null != definition) {
            return definition.getErrorCode();
        } else {
            return "";
        }
    }
    
    
}

ExceptionDefinition: Exception資訊單元

package com.ldd0.exception.config;

import java.util.LinkedHashSet;
import java.util.Set;

public class ExceptionDefinition {
    private String errorCode;

    private Set<String> handlerNames = new LinkedHashSet<String> ();

    ExceptionDefinition() {
        
    }
    
    public ExceptionDefinition(String errorCode) {
        this.errorCode = errorCode;
    }
    
    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public Set<String> getHandlerNames() {
        return handlerNames;
    }
}

ExceptionDefiniton定義了和某個exception相關的具體資訊,根據exception的class name可以從exceptionContext中的exceptionMap得到指定的exception的相關資訊,這些資訊是在系統初始化時讀取到exceptionContext中的。並且避免了exception handler的重複初始化。

可配置,受管式,方便使用

 採取兩種配置方式,exception的相關資訊比如它的errorCode, exceptionHandlers可以配置在外部的xml檔案中,也可以用annotation標註。對於exception的處理是有繼承性質的,如果某個exception沒有在exceptionContext中註冊,就使用它的父類的配置資訊。如果無任何父類在exceptionContext中註冊,就使用預設機制進行處理。

XML 方案:

            因為spring2.0支援自定義schema功能,我們可以方便地採用自己的schema只要實現NamespaceHandler和BeanDefinitionPaser,後面一個比較重要,可以將自定義xml檔案中的相關類註冊到spring的上下文中,成為spring bean。

Xml schema:

<xsd:complexType name="exceptionType">
        <xsd:sequence>
            <xsd:element name="level" default="error" minOccurs="0">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="error" />
                        <xsd:enumeration value="warning" />
                        <xsd:enumeration value="info" />
                        <xsd:enumeration value="confirmation" />
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="handler" maxOccurs="unbounded">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string" />
                </xsd:simpleType>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="errorCode">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:whiteSpace value="preserve" />
                    <xsd:pattern value="LDD600-+\d{1,5}.*" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
        <xsd:attribute name="class" type="xsd:string" use="required" />
    </xsd:complexType>

Annotation方案:

            JDK1.5以上就有了annotation,可以簡化我們的配置,使得配置資訊和程式碼聯絡在一起,增加了程式碼的可讀性。如何在spring中註冊自定義的annotation和用annotation標註的class,可以參考文章2和文章:   。對於每個註冊了的class用ExceptionalAnnotationBeanPostProcessor來parse具體的annotation資訊(對於annotation的parse方法還會在以後繼續改進)。

package com.ldd600.exception.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.ldd600.exception.annotation.Exceptional;
import com.ldd600.exception.base.BaseAppException;
import com.ldd600.exception.base.BaseAppRuntimeException;
import com.ldd600.exception.config.ExceptionDefinition;
import com.ldd600.exception.context.ExceptionContext;
import com.ldd600.exception.context.CoreContextFactory;

public class ExceptionalAnnotationBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
       if(bean instanceof BaseAppRuntimeException || bean instanceof BaseAppException) {
           Exceptional exceptional = bean.getClass().getAnnotation(Exceptional.class);
           if(null != exceptional) {
               ExceptionContext ctx = CoreContextFactory.getInstance().getExceptionContext();
               if(!ctx.containsException(bean.getClass())) {
                   ExceptionDefinition expDefinition = new ExceptionDefinition(exceptional.errorCode());
                   ctx.setExceptionDefinition(bean.getClass(), expDefinition);
               }
               ctx.addExceptionHandlers(bean.getClass(), exceptional.handlers());
               return null;
           }
       }
       return bean;
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
    }

}

結果測試:

package com.ldd600.exception.test;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.springframework.beans.factory.BeanFactory;

import com.ldd600.exception.action.BusinessAction;
import com.ldd600.exception.base.BaseAppException;
import com.ldd600.exception.base.BaseAppRuntimeException;
import com.ldd600.exception.base.ConfigException;
import com.ldd600.exception.base.handler.ConsoleHandler;
import com.ldd600.exception.context.CoreContextFactory;
import com.ldd600.exception.dto.DefaultRequest;
import com.ldd600.exception.dto.DefaultResponse;
import com.ldd600.exception.dto.Request;
import com.ldd600.exception.dto.Response;
import com.ldd600.exception.webservice.ActionBrokerImpl;

public class ExceptionTest extends DependencyInjectionExceptionTestCase {
    Mockery context = new Mockery();
    ActionBrokerImpl broker = new ActionBrokerImpl();
    final Request request = new DefaultRequest();
    final Response response = new DefaultResponse();

    @Override
    protected String[] getConfigLocations() {
        return new String[] { "applicationContext.xml" };
    }

    public void testExceptionThrow() {
        final BusinessAction<Response, Request> action = context
                .mock(BusinessAction.class);
        final BeanFactory beanFactory = context.mock(BeanFactory.class);
        assertThrowing(new Closure() {
            public void run() throws Throwable {
                context.checking(new Expectations() {
                    {
                        allowing(beanFactory).getBean("action");
                        will(returnValue(action));
                        one(action).execute(request, response);
                        will(throwException(new BaseAppException()));
                    }
                });
                broker.setExceptionHandler(new ConsoleHandler());
                broker.setBeanFactory(beanFactory);
                broker.execute("action", request, response);
            }

        }, BaseAppException.class);
    }

    public void testExceptionalAutoLoad() throws BaseAppException {
        final BeanFactory beanFactory = context.mock(BeanFactory.class);
        final BusinessAction<Response, Request> action = context
                .mock(BusinessAction.class);
        context.checking(new Expectations() {
            {
                allowing(beanFactory).getBean("action");
                will(returnValue(action));
                one(action).execute(request, response);
                will(throwException(new ConfigException()));
            }
        });
        broker.setBeanFactory(beanFactory);
        broker.execute("action", request, response);
        assertEquals(CoreContextFactory.getInstance().getExceptionContext()
                .getErrorCode(ConfigException.class), "LDD600-00002");
        context.assertIsSatisfied();
    }

    public void testRuntimeException() {
        final BusinessAction<Response, Request> action = context
                .mock(BusinessAction.class);
        final BeanFactory beanFactory = context.mock(BeanFactory.class);
        assertThrowing(new Closure() {
            public void run() throws Throwable {
                context.checking(new Expectations() {
                    {
                        allowing(beanFactory).getBean("action");
                        will(returnValue(action));
                        one(action).execute(request, response);
                        will(throwException(new BaseAppRuntimeException()));
                    }
                });
                broker.setExceptionHandler(new ConsoleHandler());
                broker.setBeanFactory(beanFactory);
                broker.execute("action", request, response);
            }

        }, BaseAppRuntimeException.class);
        // test config
        assertEquals(CoreContextFactory.getInstance().getExceptionContext()
                .getErrorCode(BaseAppRuntimeException.class), "LDD600-00001");
        // test handler
        assertFalse(response.isSuccess());
        assertEquals(response.getErrorCode(), CoreContextFactory.getInstance()
                .getExceptionContext().getErrorCode(
                        BaseAppRuntimeException.class));
        context.assertIsSatisfied();
    }

    public void testCheckedException() {
        final BusinessAction<Response, Request> action = context
                .mock(BusinessAction.class);
        final BeanFactory beanFactory = context.mock(BeanFactory.class);
        assertThrowing(new Closure() {
            public void run() throws Throwable {
                context.checking(new Expectations() {
                    {
                        allowing(beanFactory).getBean("action");
                        will(returnValue(action));
                        one(action).execute(request, response);
                        will(throwException(new ExceptionFaker()));
                    }
                });
                broker.setBeanFactory(beanFactory);
                broker.execute("action", request, response);
            }

        }, ExceptionFaker.class);
        // test config
        assertEquals(CoreContextFactory.getInstance().getExceptionContext()
                .getErrorCode(ExceptionFaker.class), "LDD600-00003");
        // test handler
        assertFalse(response.isSuccess());
        assertEquals(response.getErrorCode(), CoreContextFactory.getInstance()
                .getExceptionContext().getErrorCode(
                        ExceptionFaker.class));
        context.assertIsSatisfied();
    }
}


參考資料:

相關推薦

no