• cglib 动态代理


    JDK的动态代理比较慢,可以使用cglib的代理,速度比较快:

    package cn.demo02;

     

    import java.lang.reflect.Method;

    import java.util.zip.CheckedInputStream;

     

    import org.springframework.beans.BeansException;

    import org.springframework.beans.factory.config.BeanPostProcessor;

    import org.springframework.cglib.proxy.Enhancer;

    import org.springframework.cglib.proxy.MethodInterceptor;

    import org.springframework.cglib.proxy.MethodProxy;

    public class PostBean implements BeanPostProcessor {

             @Override

             public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

                      return bean;

             }

     

             @Override

             public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

                      if (bean instanceof Child) {

                               // 对Bean进行 cg(Code generation )lib动态代理

                               Object proxyBean = Enhancer.create(bean.getClass(), new MethodInterceptor() {

                                        @Override

                                        public Object intercept(Object proxyBean, Method method, Object[] args, MethodProxy methodProxy)

                                                          throws Throwable {

                                                 if (method.getName().equals("say")) {

                                                          System.err.println("say is beging...");

                                                          Object obj = methodProxy.invokeSuper(proxyBean, args);

                                                          System.err.println("say is over...");

                                                          return obj;

                                                 } else {

                                                          // 直斥放行

                                                          return method.invoke(bean, args);                                          }

                                        }

                               });

                               return proxyBean;

                      }

                      return bean;

             }

    }

  • 相关阅读:
    Python学习——初识类与对象
    用Python代码实现贪心问题:最优合并问题、程序存储问题、最优服务次序问题
    爬虫解析库:XPath
    python基础学习之 模块导入
    python 数据结构
    深入理解vue中的slot与slot-scope
    BeanFactory和FactoryBean的区别简介
    Spring Boot 使用Caffeine缓存
    冒泡算法和递归算法demo
    AJPFX总结方法的特点
  • 原文地址:https://www.cnblogs.com/fujilong/p/5499133.html
Copyright © 2020-2023  润新知