V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zxc1234
V2EX  ›  程序员

spring 事务是 jdk 动态代理还是 cglib 字节码织入?

  •  
  •   zxc1234 · 2020-05-01 22:01:03 +08:00 · 2544 次点击
    这是一个创建于 1427 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,请教 还是两者都有,分情况?

    9 条回复    2020-05-02 12:37:27 +08:00
    chendy
        1
    chendy  
       2020-05-01 22:11:36 +08:00
    不需要接口就能用
    那当然是 cglib 了
    ErrorMan
        2
    ErrorMan  
       2020-05-01 22:16:06 +08:00
    两种模式都支持,默认实现是 jdk 好像?
    luhe
        3
    luhe  
       2020-05-01 22:20:00 +08:00
    https://snailclimb.gitee.io/javaguide-interview/#/./docs/e-1spring

    Spring AOP 就是基于动态代理的,如果要代理的对象,实现了某个接口,那么 Spring AOP 会使用 JDK Proxy,去创建代理对象,而对于没有实现接口的对象,就无法使用 JDK Proxy 去进行代理了,这时候 Spring AOP 会使用 Cglib
    BBCCBB
        4
    BBCCBB  
       2020-05-01 22:52:53 +08:00
    jdk 动态代理和 cglib 都是用来生成代理类..

    里面的 interceptor 执行都是一个责任链的模式..
    nicklooo
        5
    nicklooo  
       2020-05-01 23:01:28 +08:00 via iPhone
    @luhe 补充一点,与 spring-context 不同,SpringBoot 默认都使用 cglib 。默认开启 cglib 的属性应该是 @EnableAspectJAutoProxy 里的 proxyTargetClass 。
    xizismile
        6
    xizismile  
       2020-05-01 23:17:44 +08:00 via Android
    1.spring 默认使用的是 jdk 动态代理
    2.如果要被代理的类没有实现任何接口 或者 配置了 proxyTargetClass 属性为 cglib 代理,才会去使用 cglib 代理
    xizismile
        7
    xizismile  
       2020-05-01 23:25:14 +08:00 via Android
    多说一些,一般这些详细的知识点在 spring 的官方文档里面都有,建议有疑惑的时候先看看官方文档,我上面的回答也是从官方文档里翻译出来的
    Aresxue
        8
    Aresxue  
       2020-05-02 10:08:33 +08:00
    spring5 默认 jdk, spring boot2 默认 cglib
    hantsy
        9
    hantsy  
       2020-05-02 12:37:27 +08:00
    这些说运行时代理吧,Spring 支持三种 weaving 方式,runtime, loading time, compiling time. AspectJ 可以实现 compiling time weaving, 将代理类什么的,先在编译的时候生成一堆 Classes 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3256 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:06 · PVG 22:06 · LAX 07:06 · JFK 10:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.