1

我在春天创建了独立应用程序。对于异常处理,我正在使用扩展 SimpleMappingExceptionResolver 类的自定义异常处理程序。

每当程序中发生异常时,我都想将其委托给特定的 java 方法。我怎么做 ?

我在网上看到了很多示例,但在 .jsp 页面上到处都进行了异常处理。我如何在 java 方法中捕获异常。

这是我的 bean 配置文件

<bean class="com.ys.core.exception.ExceptionHandler">
<property name="exceptionMappings">
<props>
<prop key="com.ys.core.exception.MyException">ExceptionHandler</prop>
<prop key="java.lang.Exception">ExceptionHandler</prop>
<prop key="java.lang.ArithmeticException">ExceptionHandler</prop>
</props>
</property>
</bean> 

<bean id="exceptionHandler" class="com.ys.core.exception.ExceptionHandler" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/com/ys/core/service/myservices" />
<property name="suffix" value=".java" />
</bean>

我可以这样做吗?意味着调用 .java 类而不是 jsp 文件?

4

2 回答 2

0

ExceptionProcessorAspect :这是一个后抛方面,它正在处理带有注释的切入点:ExceptionAnnotation

在@AfterThrowing 处理程序中,触发了基于命令模式的异常处理器

  @AfterThrowing(pointcut = "getExceptionPointcut()", throwing ="error")
public void processor(JoinPoint call,Throwable error){
    if(null!=call){
        MethodSignature signature = (MethodSignature) call.getSignature();
        Method method = signature.getMethod();
        ExceptionAnnotation myAnnotation = method.getAnnotation(ExceptionAnnotation.class);
        Class<?> value = myAnnotation.value();
        AbstractExceptionProcessor exceptionProcessor = (AbstractExceptionProcessor)this.exceptionMap.get(value);
        exceptionProcessor.processException(error);


    }
}

其他类是构成 @AfterThrwoing 方面的支持组件

1.ExceptionAnnotation:切入点的基础 2.SystemException:将从 TestExceptionClass.testEx1 抛出

于 2015-12-09T11:11:00.093 回答
-1
package com.spring;

public abstract class AbstractExceptionProcessor {





    public void processException(Throwable error){
        System.out.println("Processed "+error);
    }
}
..............................................................................

package com.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


@Component("com.spring.ExceptionProcessorAspect")
@Aspect
public class ExceptionProcessorAspect {

    //@Value("#{exceptionMap}")
    private Map<Class<?>,AbstractExceptionProcessor> exceptionMap;

    @Autowired
    @Qualifier("com.spring.SystemExceptionProcessor")
    private SystemExceptionProcessor systemExceptionProcessor;

    @Autowired
    @Qualifier("com.spring.UnsupportedExceptionProcessor")
    private UnsupportedExceptionProcessor unsupportedExceptionProcessor;

     public ExceptionProcessorAspect(){

     }


     @PostConstruct
     public void init(){
         this.exceptionMap = new HashMap<Class<?>,AbstractExceptionProcessor>();
         exceptionMap.put(SystemException.class, systemExceptionProcessor);
         exceptionMap.put(UnsupportedException.class, unsupportedExceptionProcessor);
     }
    @Pointcut("@annotation(ExceptionAnnotation)")
    public void getExceptionPointcut(){

    }

    @AfterThrowing(pointcut = "getExceptionPointcut()", throwing ="error")
    public void processor(JoinPoint call,Throwable error){
        if(null!=call){
            MethodSignature signature = (MethodSignature) call.getSignature();
            Method method = signature.getMethod();
            ExceptionAnnotation myAnnotation = method.getAnnotation(ExceptionAnnotation.class);
            Class<?> value = myAnnotation.value();
            AbstractExceptionProcessor exceptionProcessor = (AbstractExceptionProcessor)this.exceptionMap.get(value);
            exceptionProcessor.processException(error);


        }
    }

}
................................................................................................................................................................
package com.spring;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionAnnotation {

    Class<?> value();


}

................................................................................................................................................................


/**
 * 
 */
package com.spring;


public class SystemException extends RuntimeException {



    public SystemException(String message) {
        super(message);
        this.message = message;
    }

    String message;






}


................................................................................................................................................................



package com.spring;

import org.springframework.stereotype.Component;



@Component("com.spring.SystemExceptionProcessor")
public class SystemExceptionProcessor extends AbstractExceptionProcessor {






    @Override
    public void processException(Throwable error) {
        // TODO Auto-generated method stub
        System.out.println("Processed " + error.getMessage());
    }

}
................................................................................................................................................................
package com.spring;

import org.springframework.stereotype.Component;





@Component
public class TestExceptionClass {




    @ExceptionAnnotation(value=SystemException.class)
    public void testEx1(){
        System.out.println("In SystemException Block" );
        if(1==Integer.parseInt("1")){
            throw new SystemException("SystemException raised");
        }

        System.out.println("After throws Block SystemException");
    }




}
................................................................................................................................................................



/**
 * 
 */
package com.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BootClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-resources/config.xml");

        System.out.println("Spring context initialized.");



        TestExceptionClass test =  applicationContext.getBean(TestExceptionClass.class);
        test.testEx1();
    }

}
于 2015-12-09T09:58:53.497 回答