Simple Before Advice : Spring Aspect « Spring « Java






Simple Before Advice


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



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

public interface IMessageWriter {

    public void writeMessage();
}

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

public class MessageWriter implements IMessageWriter{

    public void writeMessage() {
        System.out.print("World");
    }

}
///////////////////////////////////////////////////////////////////////////////////////


import java.lang.reflect.Method;

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

public class SimpleBeforeAdvice implements MethodBeforeAdvice {

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

        // create the proxy
        ProxyFactory pf = new ProxyFactory();

        pf.addAdvice(new SimpleBeforeAdvice());
        pf.setTarget(target);

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

        // write the messages
        proxy.writeMessage();
    }

    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        System.out.println("Before method: " + method.getName());
    }

}

           
       








SimpleBeforeAdvice.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 Throws 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