公众号:架构师与哈苏
关注公众号进入it交流群! 公众号:架构师与哈苏 不定时都会推送一些实用的干货。。。

公共类

接口
Student

package com.example.demo.test.proxy;

public interface Student {

    String eat();

    String see();
}

实现类
StudentImpl

package com.example.demo.test.proxy;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *
 * </p>
 *
 * @author: GoslingWu
 * @date: 2022-03-08
 */
@Slf4j
public class StudentImpl implements Student{

    @Override
    public String eat() {
        String str = "我吃了一口🍉!";
        log.info(str);
        return str;
    }

    @Override
    public String see() {
        String str = "卧槽 海盗🐶!";
        log.info(str);
        return str;
    }


}

JDK代理

JDKProxyFactory

package com.example.demo.test.proxy.jdk;

import com.example.demo.test.proxy.Student;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <p>
 *
 * </p>
 *
 * @author: GoslingWu
 * @date: 2022-03-08
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JDKProxyFactory implements InvocationHandler {

    private Object target;

    public Student createProxy(){
        Student s = (Student) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        return s;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(target, args);
        after();
        return invoke;
    }

    private void before() {
        log.info("开始暗箱操作了!");
    }

    private void after() {
        log.info("干净又卫生,兄弟们!");
    }

}

cglib代理

CglibProxyFactory

package com.example.demo.test.proxy.cglib;

import com.example.demo.test.proxy.Student;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * <p>
 *
 * </p>
 *
 * @author: GoslingWu
 * @date: 2022-03-08
 */
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CglibProxyFactory implements MethodInterceptor {

    private Object target;


    public Student createProxy(){
        //1.创建Enhancer
        Enhancer enhancer = new Enhancer();
        //2.传递目标对象的class
        enhancer.setSuperclass(target.getClass());
        //3.设置回调操作
        enhancer.setCallback(this);
        return (Student) enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object invoke = methodProxy.invoke(target, objects);
        after();
        return invoke;
    }

    private void before() {
        log.info("那是个啥玩意,走近看看。。");
    }

    private void after() {
        log.info("走开。。。");
    }


}
posted on 2022-03-08 15:57  公众号/架构师与哈苏  阅读(20)  评论(0编辑  收藏  举报