3-05.cglib-实现AOP编程讲解

 

 

 

 

 

MyBeanFactory .java

复制代码
package com.gyf.factory;

import com.gyf.aspect.MyAspect2;
import com.gyf.service.IUserService;
import com.gyf.service.StudentService;
import com.gyf.service.UserServiceImpl;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

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

public class MyBeanFactory {

    /**
     * JDK实现代理
     * @return
     */
    public static IUserService createUserService(){
        //1.创建目标对象target
        final IUserService userService = new UserServiceImpl();

        //2.声明切面类对象
        final MyAspect2 aspect = new MyAspect2();

        //3.把切面类2个方法 应用 目标类
        //3.1 创建JDK代理,拦截方法
        /*newProxyInstance(
                ClassLoader loader, 类加载器,写当类
                Class<?>[] interfaces, 接口,接口的方法会被拦截
                InvocationHandler h) 处理
                */
        IUserService seriviceProxy = (IUserService) Proxy.newProxyInstance(
                MyBeanFactory.class.getClassLoader(),
                userService.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //开启事务
                        //aspect.before();
                        aspect.before();

                        //方法返回值是 业务方法的返回值
                        Object retObj = method.invoke(userService,args);
                        //System.out.println("拦截返回值:" + retObj);

                        //提交事务
                        aspect.after();
                        return retObj;
                    }
                }
        );

        return seriviceProxy;
    }

    /**
     * cglib实现代理
     * @return
     */
    public static StudentService createStudentService(){
        //1.创建目标对象target
        final StudentService studentService = new StudentService();

        //2.声明切面类对象
        final MyAspect2 aspect = new MyAspect2();

        //3.创建增强对象
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(studentService.getClass());
        //设置回调【拦截】
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                /**
                 * proxy:
                 * om.gyf.service.StudentService$$EnhancerByCGLIB$$fbb8ef26
                 * proxy代理对象是StudentService的子类
                 */
                //System.out.println(proxy);
                //System.out.println(methodProxy);

                aspect.before();

                //放行方法
                //Object retObj = method.invoke(studentService,args);
                Object retObj = methodProxy.invokeSuper(proxy,args);//解藕
                System.out.println("拦截.....");

                //System.out.println(proxy);
                //System.out.println(methodProxy);
                aspect.after();
                return retObj;
            }
        });

        //创建代理对象
        StudentService serviceProxy = (StudentService) enhancer.create();
        //System.out.println("serviceProxy);
        return serviceProxy;
    }
}
复制代码

 

 

 

 

 

 

 

posted @   expworld  阅读(157)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示