• aop的四种增强以及JDK动态代理、Cglib动态代理


    动态代理 AOP底层实现:有接口自动应用的就是JDK动态代理
    (1).JDK 在运行时运行时注入
    本质:在内存中构建出接口的实现类
    特点:被代理对象,必须有接口

    实例:

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    public class Test {
        public static void main(String[] args) {
          final SomeService service = new SomeServiceImpl();
    
          SomeService proxy =(SomeService)Proxy.newProxyInstance(service.getClass().getClassLoader(),
                  service.getClass().getInterfaces(), new InvocationHandler() {
                      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                          System.out.println("before=======");
                          method.invoke(service,args);
    System.out.println("after========");
    return null; } }); proxy.doSome(); } }

    Cglib 底层,注入,编译期已经注入了
    本质:在内存中生成被代理类(目标类)的【子类】
    特点:可以在没有接口的情况下代理
    对于不使用接口的业务类,无法使用JDK动态代理,cglib采用非常底层的字节码技术,
    可以为一个类创建子类

    import cn.jdkdl.SomeServiceImpl;
    import org.springframework.cglib.proxy.Enhancer;
    import org.springframework.cglib.proxy.MethodInterceptor;
    import org.springframework.cglib.proxy.MethodProxy;
    
    import java.lang.reflect.Method;
    
    
    public class Test {
        public static void main(String[] args) {
            final SomeServiceImpl someService = new SomeServiceImpl();
    
            Enhancer enhancer = new Enhancer();
    
            enhancer.setSuperclass(someService.getClass());
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    System.out.println("=======before");
                    methodProxy.invoke(someService,objects);
                    return null;
                }
            });
            SomeServiceImpl proxy = (SomeServiceImpl) enhancer.create();
            proxy.doSome();
        }
    }

    接下来是四种增强

    前置增强 MethodBeforeAdvice

    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterAdvice implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("after========");
        }
    }
    <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean>
    <bean id="beforeAdvice" class="cn.jdkdl.BeforeAdvice"></bean> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="service"></property> <property name="interceptorNames" value="beforeAdvice"></property> </bean>

    后置增强 AfterReturingAdvice

    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterAdvice implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("after========");
        }
    }

    xml文件内容

       <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean>
    
        <bean id="afterAdvice" class="cn.jdkdl.AfterAdvice"></bean>
    
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target" ref="service"></property>
            <property name="interceptorNames" value="afterAdvice"></property>
        </bean>

    3.环绕增强

    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    public class HrAdvice implements MethodInterceptor {
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            System.out.println("before====");
            methodInvocation.proceed();
            System.out.println("after====");
            return null;
        }
    }

    xml文件

       <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean>
    
        <bean id="hrAdvice" class="cn.jdkdl.HrAdvice"></bean>
    
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target" ref="service"></property>
            <property name="interceptorNames" value="hrAdvice"></property>
        </bean>

    异常增强

    public class MyThrowsAdvice implements ThrowsAdvice {
        public void afterThrowing(Exception e){
            System.out.println("网络异常......");
        }
    }
       <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean>
    
        <bean id="throwsAdvice" class="cn.jdkdl.MyThrowsAdvice"></bean>
    
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target" ref="service"></property>
            <property name="interceptorNames" value="throwsAdvice"></property>
        </bean>
  • 相关阅读:
    那些年,加班搞过的需求...
    在基于nuxt的移动端页面中引用mint UI的popup组件之父子组件传值
    nuxt下运行项目时内存溢出(out of memory)的一种情况
    转载的
    VUE旅程(2)
    用windbg+sos找出程序中谁占用内存过高,谁占用CPU过高(转载)
    分块算法总结(持续更新ing)
    【MATLAB建模学习实录【三】】插值与拟合、数据处理
    【MATLAB建模学习实录【二】】非线性规划函数模型板子
    【MATLAB建模学习实录【一】】MATLAB求解线性规划模板
  • 原文地址:https://www.cnblogs.com/ztm1021810064/p/8539083.html
Copyright © 2020-2023  润新知