闽公网安备 35020302035485号
//目标对象
public class UserImpl {
public void system(){
System.out.println("输出测试");
}
}
//代理对象
public class Proxy extends UserImpl {
@Override
public void system() {
super.system();
System.out.println("增强之后的输出");
}
}
// 堆代码 duidaima.com
//测试类
public class TestMain {
public static void main(String[] args) {
UserImpl user = new Proxy();
user.system();
}
}
静态代理可以看出来一点问题吧?每次代理都要实现一个类,导致项目中代码很多;你每次想要代理,都要去实现一个类,代码就会成堆的增加,然后你就会发现项目的类就会越来越多,就会导致你们的项目显得很臃肿。而且代码的复用性太低了,并且耦合度非常高,这个我们所说的高内聚低耦合是相悖的。//接口类
public interface Italk {
public void talk(String msg);
}
//实现类
public class People implements Italk {
public String username;
public String age;
public String getName() {
return username;
}
public void setName(String name) {
this.username= name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public People(String name1, String age1) {
this.username= name1;
this.age = age1;
}
public void talk(String msg) {
System.out.println(msg+"!你好,我是"+username+",我年龄是"+age);
}
}
//代理类
public class TalkProxy implements Italk {
Italk talker;
public TalkProxy (Italk talker) {
//super();
this.talker=talker;
}
public void talk(String msg) {
talker.talk(msg);
}
public void talk(String msg,String singname) {
talker.talk(msg);
sing(singname);
}
private void sing(String singname){
System.out.println("唱歌:"+singname);
}
}
//测试
public class MyProxyTest {
//代理模式
public static void main(String[] args) {
//不需要执行额外方法的
Italk people1=new People("湖海散人","18");
people1.talk("No ProXY Test");
System.out.println("-----------------------------");
//需要执行额外方法的
TalkProxy talker=new TalkProxy(people1);
talker.talk("ProXY Test","七里香");
}
}
代码解析如下:/**
* 业务类,
*/
public class TestService {
public TestService() {
System.out.println("TestService的构造");
}
/**
* 该方法不能被子类覆盖,Cglib是无法代理final修饰的方法的
*/
final public String sayOthers(String name) {
System.out.println("TestService:sayOthers>>"+name);
return null;
}
public void sayHello() {
System.out.println("TestService:sayHello");
}
}
/**
* 自定义MethodInterceptor
*/
public class MethodInterceptorTest implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("======插入前置通知======");
Object object = methodProxy.invokeSuper(o, objects);
System.out.println("======插入后者通知======");
return object;
}
}
/**
* 测试
*/
public class Client {
public static void main(String[] args) {
// 代理类class文件存入本地磁盘方便我们反编译查看源码
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");
// 通过CGLIB动态代理获取代理对象的过程
Enhancer enhancer = new Enhancer();
// 设置enhancer对象的父类
enhancer.setSuperclass(TestService.class);
// 设置enhancer的回调对象
MethodInterceptorTest t = new MethodInterceptorTest();
enhancer.setCallback(t);
// 创建代理对象
TestService proxy= (TestService)enhancer.create();
// 通过代理对象调用目标方法
proxy.sayHello();
}
}
运行结果CGLIB debugging enabled, writing to 'D:\code' TestService的构造 ======插入前置通知====== TestService:sayHello ======插入后者通知======实现CGLIB动态代理必须实现MethodInterceptor(方法拦截器)接口,这个接口只有一个intercept()方法,这个方法有4个参数:
proxy表示要触发父类的方法对象;