File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
public class Main {
public static void main(String[] args) throws Exception {
MessageWriter target = new MessageWriter();
ProxyFactory pf = new ProxyFactory();
pf.addAdvice(new SimpleBeforeAdvice());
pf.setTarget(target);
MessageWriter proxy = (MessageWriter) pf.getProxy();
proxy.writeMessage();
}
}
class MessageWriter {
public void writeMessage() {
System.out.println("A");
}
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("Before method: " + method.getName());
}
}
Download: Spring-MethodBeforeAdvice.zip( 4,744 k)