import org.springframework.aop.framework.ProxyFactory;
public class BeforeExample {
public static void main(String[] args) {BeforeExample a = new BeforeExample();
KeyGenerator keyGen = a.getKeyGenerator();
keyGen.setKey(8888,"aaaaaaaaa");//传入参数
keyGen.aa();
long key = keyGen.getKey();
System.out.println("Key: " + key);
}
private KeyGenerator getKeyGenerator() {//AOP 主方法
KeyGenerator target = new KeyGenerator();
ProxyFactory factory = new ProxyFactory();
factory.setTarget(target);
factory.addAdvice(new SimpleBeforeAdvicea());
return (KeyGenerator) factory.getProxy();
}
}
public class KeyGenerator {
private long value;
private String name;
public long getKey() {
return value;
}
public void setKey(long value,String name) {
this.value = value;
this.name=name;
}
public void aa() {
System.out.println("mmmmmmmmmmmm");
}
}
package com.hdsx;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class SimpleBeforeAdvicea implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
KeyGenerator o = (KeyGenerator) target;//需要织入的目标类
for(int i=0;i<args.length;i++){//args传的的参数
System.out.println(method.getName()+"+++++,"+o.getKey());
if(args[i] instanceof Long){
Long os = (Long)args[i];//类型转换
System.out.println(os);
}
if(args[i] instanceof String){
String os = (String)args[i];//类型转换
System.out.println(os);
}
}
}
}