Simple Throws Advice : Spring Aspect « Spring « Java






Simple Throws Advice

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/

///////////////////////////////////////////////////////////////////////////////////////
public class ErrorBean {

    public void errorProneMethod() throws Exception {
        throw new Exception("Foo");
    }

    public void otherErrorProneMethod() throws IllegalArgumentException {
        throw new IllegalArgumentException("Bar");
    }
}

///////////////////////////////////////////////////////////////////////////////////////

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class SimpleThrowsAdvice implements ThrowsAdvice {

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

        ProxyFactory pf = new ProxyFactory();
        pf.setTarget(errorBean);
        pf.addAdvice(new SimpleThrowsAdvice());

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

        try {
            proxy.errorProneMethod();
        } catch (Exception ignored) {

        }

        try {
            proxy.otherErrorProneMethod();
        } catch (Exception ignored) {

        }

    }

    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");
    }

    public void afterThrowing(Method method, Object[] args, Object target,
            IllegalArgumentException ex) throws Throwable {
        System.out.println("***");
        System.out.println("IllegalArgumentException Capture");
        System.out.println("Caught: " + ex.getClass().getName());
        System.out.println("Method: " + method.getName());
        System.out.println("***\n");
    }
}




           
       








SimpleThrowsAdvice.zip( 1,478 k)

Related examples in the same category

1.Profiling Example
2.Introduction Config Example
3.Security Example
4.Simple After Returning Advice
5.Simple Before Advice
6.Composable Pointcut Example
7.Control Flow Example
8.Dynamic Pointcut Example
9.Hello World With Pointcut
10.Spring Aspect Introduction Example
11.Static Pointcut Example
12.Name Pointcut Example
13.Name Pointcut Using Advisor
14.Proxy Factory Bean Example
15.Proxy Perf Test
16.Regexp Pointcut Example
17.After Advice Example
18.AspectJ Example from Pro Spring
19.Aspect Hello World Example