第一:一个配置文件config.properties写道
#*** = java.util.ArrayList
*** = com.css.aopframework.ProxyFactoryBean
***.advice = com.css.aopframework.MyAdvice
***.target =java.util.ArrayList
*** = com.css.aopframework.ProxyFactoryBean
***.advice = com.css.aopframework.MyAdvice
***.target =java.util.ArrayList
写道
package com.css.aopframework;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactoryBean {
private Object target ;
private Advice advice ;
public Object getProxy() {
Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(),
/*new Class[]{Collection.class},*/
target.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
advice.beforeMehod(method);
Object returnValue = method.invoke(target, args);
advice.afterMehod(method);
return returnValue;
}
});
return proxy;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Advice getAdvice() {
return advice;
}
public void setAdvice(Advice advice) {
this.advice = advice;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactoryBean {
private Object target ;
private Advice advice ;
public Object getProxy() {
Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(),
/*new Class[]{Collection.class},*/
target.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
advice.beforeMehod(method);
Object returnValue = method.invoke(target, args);
advice.afterMehod(method);
return returnValue;
}
});
return proxy;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Advice getAdvice() {
return advice;
}
public void setAdvice(Advice advice) {
this.advice = advice;
}
}
写道
package com.css.aopframework;
import java.lang.reflect.Method;
public interface Advice {
/**
*
*/
void beforeMehod(Method method);
/**
*
*/
void afterMehod(Method method);
}
import java.lang.reflect.Method;
public interface Advice {
/**
*
*/
void beforeMehod(Method method);
/**
*
*/
void afterMehod(Method method);
}
写道
package com.css.aopframework;
import java.lang.reflect.Method;
public class MyAdvice implements Advice {
long beginTime = 0;
public void afterMehod(Method method) {
System.out.println(">>>>>>>>>>>>>>:after method ");
long endTime = System.currentTimeMillis();
System.out.println(method.getName()+" running time is :"+ (endTime-beginTime));
}
public void beforeMehod(Method method) {
System.out.println(">>>>>>>>>>>>>>:before method ");
beginTime = System.currentTimeMillis();
}
}
import java.lang.reflect.Method;
public class MyAdvice implements Advice {
long beginTime = 0;
public void afterMehod(Method method) {
System.out.println(">>>>>>>>>>>>>>:after method ");
long endTime = System.currentTimeMillis();
System.out.println(method.getName()+" running time is :"+ (endTime-beginTime));
}
public void beforeMehod(Method method) {
System.out.println(">>>>>>>>>>>>>>:before method ");
beginTime = System.currentTimeMillis();
}
}