一个简单的spring AOP的实现

第一:一个配置文件config.properties写道

#*** = 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;
}

}

 

 写道
package com.css.aopframework;

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();
}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值