@InvokedErrorProxy

Posted by Bruce Tsai

說明

透過 AOP 記錄呼叫方法時,發生錯誤時的輸入及錯誤堆疊。需在 spring 設定中設定 AOP 相關的設定,配合 InvokedErrorAdvice 或 InvokedErrorChainAdvice 使用。

範例

  • spring 設定檔
<aop:config proxy-target-class="true">

    <aop:pointcut
      id="invokedErrorProxy"
      expression="execution(* com.fet.crm.eservice..*.*(..))
      and (@within(foundation.core.generic.annotation.aop.InvokedErrorProxy)
      or @annotation(foundation.core.generic.annotation.aop.InvokedErrorProxy))" />

    <aop:advisor
      advice-ref="invokedErrorAdvice" 
      pointcut-ref="invokedErrorProxy" />

</aop:config>

<bean id="invokedErrorAdvice"
      class="foundation.core.generic.aop.InvokedErrorAdvice">
    <property name="enabled" value="true" />
</bean>
  • 程式用例
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Executor {

    @InvokedErrorProxy
    public void execute(double c) {
        Logs.info("執行execute");

        throw new RuntimeException("無相關權限");
    }

}


public class Program

    public static void main(String[] args) {
        Context context = Contexts.Load();
        Executor executor = context.getBean(Executor.class);
        executor.execute(Math.random());
    }
}

輸出結果

**********************執行序列**********************
public void com.prhythm.test.generic.aop.InvokeErrorAdviceTest&#x24;Executor.execute(double)
輸入參數 => {
    [double] 0.08238830328758562
}
java.lang.RuntimeException: 無相關權限
    at com.prhythm.test.generic.aop.InvokeErrorAdviceTest&#x24;Executor.execute(InvokeErrorAdviceTest.java:32)
    at com.prhythm.test.generic.aop.InvokeErrorAdviceTest&#x24;Executor&#x24;&#x24;FastClassBySpringCGLIB&#x24;&#x24;f6ff25a7.invoke(&tl;generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy&#x24;CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at com.prhythm.core.generic.aop.InvokedErrorChainAdvice.invoke(InvokedErrorChainAdvice.java:105)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy&#x24;DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.prhythm.test.generic.aop.InvokeErrorAdviceTest&#x24;Executor&#x24;&#x24;EnhancerBySpringCGLIB&#x24;&#x24;ea6dec4d.execute(<generated>)
    at com.prhythm.test.generic.aop.InvokeErrorAdviceTest.testThrowException(InvokeErrorAdviceTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    ..........

results matching ""

    No results matching ""