静态代理在使用时,需要定义接口或者父类,被代理对象(即目标对象)与代理对象一起实现相同的接口或者是继承相同父类
package com.liyang;
// 1. 教师的接口, 因为可能会出很多的教师, 语文 数学, 这里做一个抽象
public interface ITeacherDao {
// 所有的教师共有的方法 教书
public void teach();
}
package com.liyang;
// 2. 教师类 实现ITeacherDao
public class TeacherDao implements ITeacherDao {
@Override
public void teach() {
System.out.println("教书ing...");
}
}
package com.liyang;
// 3. 现在的需要要求上课之前先点名, 因此做这个代理类来实现功能
public class TeacherDaoProxy implements ITeacherDao {
// 目标对象
private ITeacherDao target;
public TeacherDaoProxy(ITeacherDao target) {
super();
this.target = target;
}
@Override
public void teach() {
System.out.println("同学们, 现在我们点名...");
target.teach();
}
}
package com.liyang;
// 4. 测试
public class Client {
public static void main(String[] args) {
ITeacherDao teacherDao = new TeacherDaoProxy(new TeacherDao());
teacherDao.teach();
}
}
static Object newProxyInstance(ClassLoader loader, Class<?>[]
interfaces,InvocationHandler h )
package com.liyang;
// 1. 教师的接口, 因为可能会出很多的教师, 语文 数学, 这里做一个抽象
public interface ITeacherDao {
// 所有的教师共有的方法 教书
public void teach();
}
package com.liyang;
// 2. 教师类 实现ITeacherDao
public class TeacherDao implements ITeacherDao {
@Override
public void teach() {
System.out.println("教书ing...");
}
}
package com.liyang;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 3. 进行动态代理
public class ProxyFactory {
private Object target;
public ProxyFactory(Object target) {
super();
this.target = target;
}
public Object getProxyInstance() {
return Proxy.newProxyInstance(
// 目标类的类加载器
target.getClass().getClassLoader(),
// 目标类的实现接口类型
target.getClass().getInterfaces(),
// 事件处理
new InvocationHandler() {
@Override
public Object invoke(Object proixy, Method method, Object[] args) throws Throwable {
System.out.println("老师开始点名了...");
// 反射机制调用目标方法
Object returnValue = method.invoke(target, args);
return returnValue;
}
}
);
}
}
package com.liyang;
// 4. 测试
public class Client {
public static void main(String[] args) {
ITeacherDao teacherDao = (ITeacherDao)new ProxyFactory(new TeacherDao())
.getProxyInstance();
teacherDao.teach();
// class com.sun.proxy.$Proxy0 内存中动态生成代理对象
System.out.println(teacherDao.getClass());
}
}
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>3.2.4version>
dependency>
package com.liyang;
// 1. 教师类
public class TeacherDao implements ITeacherDao {
@Override
public void teach() {
System.out.println("教书ing...");
}
}
package com.liyang;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
// 2. 进行动态代理
public class ProxyFactory implements MethodInterceptor {
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
// 返回一个代理对象
public Object getProxyInstance() {
// 1. 创建工具类
Enhancer enhancer = new Enhancer();
// 2. 设置父类
enhancer.setSuperclass(target.getClass());
// 3. 设置回调函数
enhancer.setCallback(this);
// 4. 创建子类对象, 即代理对象
return enhancer.create();
}
// 调用目标对象的相关方法
@Override
public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3) throws Throwable {
System.out.println("开始点名了...");
Object value = method.invoke(target, args);
System.out.println("代理完成...");
return value;
}
}
package com.liyang;
// 3. 测试
public class Client {
public static void main(String[] args) {
ITeacherDao teacherDao = (ITeacherDao)new ProxyFactory(new TeacherDao())
.getProxyInstance();
teacherDao.teach();
// class com.sun.proxy.$Proxy0 内存中动态生成代理对象
System.out.println(teacherDao.getClass());
}
}