Name Pointcut Using Advisor : Spring Aspect « Spring « Java






Name Pointcut Using Advisor

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



///////////////////////////////////////////////////////////////////////////////////////
public class NameBean {

    public void foo() {
        System.out.println("foo");
    }
    
    public void foo(int x) {
        System.out.println("foo " + x);
    }
    
    public void bar() {
        System.out.println("bar");
    }
    
    public void yup() {
        System.out.println("yup");
    }
}


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

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SimpleAdvice implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println(">> Invoking " + invocation.getMethod().getName());
        Object retVal = invocation.proceed();
        System.out.println(">> Done");
        return retVal;
    }

}

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

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;

public class NamePointcutUsingAdvisor {

    public static void main(String[] args) {
        NameBean target = new NameBean();

        // create advisor
        NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(
                new SimpleAdvice());
        advisor.addMethodName("foo");
        advisor.addMethodName("bar");

        // create the proxy
        ProxyFactory pf = new ProxyFactory();
        pf.setTarget(target);
        pf.addAdvisor(advisor);
        NameBean proxy = (NameBean) pf.getProxy();

        proxy.foo();
        proxy.foo(999);
        proxy.bar();
        proxy.yup();
    }
}



           
       








NamePointcutUsingAdvisor.zip( 1,479 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.Simple Throws Advice
7.Composable Pointcut Example
8.Control Flow Example
9.Dynamic Pointcut Example
10.Hello World With Pointcut
11.Spring Aspect Introduction Example
12.Static Pointcut Example
13.Name Pointcut Example
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