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之后,切入点的类需要实现某个接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值