当前位置: 首页 > article >正文

Spring 动态代理 JDK代理和GGLIB代理

在Spring AOP中默认是使用的JDK动态代理,该动态代理要求目标类必须实现接口,但是GGLIB没有这个要求

我的beans.xml配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 定义目标对象 -->

    <!-- 定义切面 -->
    <bean id="logAspect" class="com.aspect.LogAspect" />

    <bean id="studentService" class="com.service.impl.StudentServiceImpl" />

    <!-- 配置AOP -->
    <aop:config >
        <!-- 定义通知 -->
       <aop:aspect id="myAspect" ref="logAspect">
            <aop:pointcut id="pointCut" expression="execution(* com.service.impl..*.*(..))" />
            <aop:before method="beforePrintLog" pointcut-ref="pointCut" />
        </aop:aspect>
    </aop:config>
</beans>

 对于:StudentServiceImpl我没有实现任何的接口,我认为这个这里应该会默认使用GGLIB动态代理

public class StudentServiceImpl  {
	
	
	public void study() {
		System.out.println("正在学习...............");
	}

}

但是却出现下面的错误:

Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @6404f418
	at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:464)
	at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
	at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
	at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
	at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
	at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221)
	at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:174)
	at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:153)
	at org.springframework.cglib.proxy.Enhancer.<clinit>(Enhancer.java:73)
	... 20 more

可以看到这个报错确实是使用了GGLIB这个动态代理,但是为什么还是出现错误

难道是因为对应的版本不兼容

我配置了依赖:

   <dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.3.0</version>
</dependency>

 还是不行,然后我看了我的jdk版本,当前用的是17,我切换到1.8 发现可以了

二月 17, 2025 7:33:20 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460: startup date [Mon Feb 17 19:33:20 CST 2025]; root of context hierarchy
二月 17, 2025 7:33:20 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
LogAspectbeforePrintLog
正在学习...............

 所以我判断GGLib动态代理在jdk17之前是正常的,切入点的类可以不用实现某个接口,但是到17之后,切入点的类需要实现某个接口


http://www.kler.cn/a/552591.html

相关文章:

  • 前端编程基础开发规范
  • 【练习】【滑动窗口】力扣热题100 438. 找到字符串中所有字母异位词
  • 从插入排序到希尔排序
  • A与B组件自动对齐与组装,无映射直接补偿。
  • SpingBoot-Vue 前后端分离—实现钉钉免登功能(2025)
  • 【Spring+MyBatis】_图书管理系统(中篇)
  • c# -01新属性-模式匹配、弃元、析构元组和其他类型
  • spark大数据分析
  • python-leetcode-最小路径和
  • vue中使用富文本编辑器
  • SpringBoot速成(14)文件上传P23-P26
  • GIT:如何合并已commit的信息并进行push操作
  • 【达梦数据库】dblink连接[SqlServer/Mysql]报错处理
  • Edge浏览器清理主页
  • Copilot Next Edit Suggestions(预览版)
  • nodejs:express + js-mdict 网页查询英汉词典,能显示图片
  • 【Java 面试 八股文】并发编程篇
  • The First项目报告:重塑链上游戏生态,解读B3 Base的双赢局面
  • Pytorch实现之粒子群优化算法在GAN中的应用
  • 一周学会Flask3 Python Web开发-post请求与参数获取