public interface StuManger {
public void addStudent();
public void delStudent();
public void select();
}
public class StuMangerImpl implements StuManger{
@Override
public void addStudent() {
// TODO Auto-generated method stub
System.out.println("正在添加学生");
}
@Override
public void delStudent() {
// TODO Auto-generated method stub
System.out.println("正在删除学生");
}
@Override
public void select() {
// TODO Auto-generated method stub
System.out.println("正在查找");
}
}
public class LogHander implements InvocationHandler{
//目标对象
Object target;
public LogHander() {
// TODO Auto-generated constructor stub
}
public LogHander(Object target){
this.target=target;
}
public Object getProxy(){
return Proxy.newProxyInstance
(target.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
log("调用方法前");
Object objectValue = method.invoke(target, args);//调用目标方法
log("调用方法后");
return objectValue;
}
public void log(String msg){
System.out.println("日志代理"+msg);
}
}
public class Test {
public static void main(String[] args) {
StuManger manger = new StuMangerImpl();
LogHander hander =new LogHander(manger);
StuManger manger2 = (StuManger)hander.getProxy();
manger2.addStudent();
}
}