SpringAop动态代理和AspectJ静态代理

一、SpringAop动态代理(运行时增强)

SpringAop如何使用代理取决于代理的对象是否实现了接口,如果实现接口,则默认使用jdk动态代理,当然也可以让其强制使用cglib代理。如果没有实现了接口,则必须使用cglib代理。总之spring会自动在JDK动态代理和CGLIB之间进行转换转换。通过注解@EnableAspectJAutoProxy

1、jdk代理

java 复制代码
package com.example.springboottest.proxy;

public interface ProxyService {
    public abstract void targetMethod();
}
java 复制代码
package com.example.springboottest.proxy;

public class JdkProxyServiceImpl implements ProxyService {
    @Override
    public void targetMethod() {
        System.out.println("目标方法执行");
    }
}
java 复制代码
package com.example.springboottest.proxy;

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

public class ProxyHandler implements InvocationHandler {

    private Object proxyObj;

    public ProxyHandler(Object proxyObj) {
        this.proxyObj = proxyObj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnParam = null;
        try{
            System.out.println("before前置通知");
            returnParam =  method.invoke(proxyObj,args);
            System.out.println("afterReturning返回通知");
        }catch (Exception e){
            System.out.println("afterThrowing异常通知");
        }finally {
            System.out.println("after后置通知");
        }
        return returnParam;
    }

    public static void main(String[] args) {
        ProxyService jdkProxyService = new JdkProxyServiceImpl();
        ProxyService proxyService = (ProxyService) Proxy.newProxyInstance(
                jdkProxyService.getClass().getClassLoader(),
                jdkProxyService.getClass().getInterfaces(),
                new ProxyHandler(jdkProxyService));
        proxyService.targetMethod();
    }
}

2、cglib代理

java 复制代码
package com.example.springboottest.proxy;

public class CglibServiceImpl {

    public void targetMethod(){
        System.out.println("目标方法执行");
    }
}
java 复制代码
package com.example.springboottest.proxy;

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

public class ProxyHandler implements InvocationHandler {

    private Object proxyObj;

    public ProxyHandler(Object proxyObj) {
        this.proxyObj = proxyObj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnParam = null;
        try{
            System.out.println("before前置通知");
            returnParam =  method.invoke(proxyObj,args);
            System.out.println("afterReturning返回通知");
        }catch (Exception e){
            System.out.println("afterThrowing异常通知");
        }finally {
            System.out.println("after后置通知");
        }
        return returnParam;
    }

    public static void main(String[] args) {
        ProxyService jdkProxyService = new JdkProxyServiceImpl();
        ProxyService proxyService = (ProxyService) Proxy.newProxyInstance(
                jdkProxyService.getClass().getClassLoader(),
                jdkProxyService.getClass().getInterfaces(),
                new ProxyHandler(jdkProxyService));
        proxyService.targetMethod();
    }
}

二、Aspectj静态代理(编译时增强)

AspectJ的底层技术是静态代理 ,即用一种 AspectJ 支持的特定语言编写切面,通过一个命令来编译,生成一个新的 代理类,该代理类增强了业务类,这是在编译时增强,相对于上面的运行时增强,编译时增强的性能更好。

三、区别

  • 静态代理在编译期就确定了代理类,而动态代理需要靠反射机制动态生成代理类,即AspectJ在编译时就增强了目标对象,Spring AOP的动态代理则是在每次运行时动态的增强,生成AOP代理对象。
  • 生成代理对象的时机不同,相对来说AspectJ的静态代理方式具有更好的性能,但是AspectJ需要特定的编译器进行处理,而Spring AOP则无需特定的编译器处理。

baijiahao.baidu.com/s?id=174110...

相关推荐
Linn3 小时前
Spring WebSocket 服务实现的主流方案与最佳实践
spring boot·后端·spring
NE_STOP3 小时前
SpringBoot--如何整体读取多个配置属性及其相关操作
java·spring
二饭6 小时前
解决Maven“无法将类 XXXXX 中的构造器 XXXXXX 应用到给定类型”错误
java·spring·maven
努力的小郑6 小时前
Spring监听器(ApplicationEvent):比MQ更轻的异步神器!亿级流量下的咖啡店经营哲学
java·后端·spring
Cyanto16 小时前
Spring注解IoC与JUnit整合实战
java·开发语言·spring·mybatis
qq_4338889316 小时前
Junit多线程的坑
java·spring·junit
gadiaola16 小时前
【SSM面试篇】Spring、SpringMVC、SpringBoot、Mybatis高频八股汇总
java·spring boot·spring·面试·mybatis
写不出来就跑路16 小时前
WebClient与HTTPInterface远程调用对比
java·开发语言·后端·spring·springboot
麦兜*17 小时前
Spring Boot 集成Reactive Web 性能优化全栈技术方案,包含底层原理、压测方法论、参数调优
java·前端·spring boot·spring·spring cloud·性能优化·maven
绿蚁新亭1 天前
Spring的事务控制——学习历程
数据库·学习·spring