我在春天创建了独立应用程序。 对于异常处理我正在使用自定义异常处理程序,它扩展了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文件?
答案 0 :(得分:0)
ExceptionProcessorAspect:它是一个抛出后的方面,它处理带有注释的Pointcut: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
抛出答案 1 :(得分:-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();
}
}