2017-05-08 3 views
1

メソッド全体を囲むSpring Aspect(アノテーションは@ExceptionTranslationなど)を作成し、このメソッドをtry ... catchメソッドに入れるにはどうすればよいですか?Spring Aspect:try catchで全体を囲む方法

@ExceptionTranslation 
public void method() { 

    // do some stuff here... 
} 

ので、論理的にそれはありません:

public void method() { 
    try { 
     // do some stuff here ... 
    } catch(Exception ex) { 
    } 
} 
+1

@AfterThrowing面を見てください。ここに例がありますhttp://howtodoinjava.com/spring/spring-aop/aspectj-afterthrowing-annotation-example/ – pvpkiran

答えて

0

あなたはあなたの問題を解決AfterThrowsアドバイスの実装例を見ることができます。

CustomException.java

package com.yourpackage; 

public class CustomException extends Exception { 

    public CustomException(final Throwable cause) { 
     super(cause); 
    } 
} 

ErrorBean.java

package com.yourpackage; 

public class ErrorBean { 

    @ExceptionTranslation 
    public void translatedException() throws Exception { 
     throw new Exception("Foo"); 
    } 


    public void notTranslatedException() throws Exception { 
     throw new Exception("Bar"); 
    } 
} 

ExceptionTranslation.java

package com.yourpackage; 

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

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface ExceptionTranslation { 
} 

SimpleThrowsAdvice.java

package com.yourpackage; 

import org.springframework.aop.Advisor; 
import org.springframework.aop.ThrowsAdvice; 
import org.springframework.aop.framework.ProxyFactory; 
import org.springframework.aop.support.DefaultPointcutAdvisor; 
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; 

public class SimpleThrowsAdvice implements ThrowsAdvice { 

    public static void main(String[] args) throws Exception { 
     ErrorBean errorBean = new ErrorBean(); 

     AnnotationMatchingPointcut pc = AnnotationMatchingPointcut.forMethodAnnotation(ExceptionTranslation.class); 

     SimpleThrowsAdvice advice = new SimpleThrowsAdvice(); 
     Advisor advisor = new DefaultPointcutAdvisor(pc, advice); 

     ProxyFactory pf = new ProxyFactory(); 
     pf.setTarget(errorBean); 
     pf.addAdvisor(advisor); 

     ErrorBean proxy = (ErrorBean) pf.getProxy(); 

     try { 
      proxy.translatedException(); 
     } catch (CustomException ex) { 
      System.out.println("CustomException caught"); 
     } catch (Exception ex) { 
      System.out.println("Exception caught"); 
     } 

     try { 
      proxy.notTranslatedException(); 
     } catch (CustomException ex) { 
      System.out.println("CustomException caught"); 
     } catch (Exception ex) { 
      System.out.println("Exception caught"); 
     } 
    } 

    public void afterThrowing(Exception ex) throws Throwable { 
     System.out.println("***"); 
     System.out.println("Generic Exception Capture"); 
     System.out.println("Caught: " + ex.getClass().getName()); 
     System.out.println("***\n"); 

     throw new CustomException(ex); 
    } 

} 
関連する問題