If using @target in the pointcut expression, spring will create proxy for the classed which are in maybe case and will check the aop when user call the functions of proxy class. If there's a final class in these proxy functions. the project will start failed. So I return the target object, for these final class without proxy.
Comment From: snicoll
@caoxuhao can you please stop closing and opening duplicate PRs as it generates unnecessary noise for the 3.5K watchers on this repository. If you want to amend your PR, you need to push additional commit on your branch and this PR will update itself. There is a hint on this page:
Add more commits by pushing to the main branch on caoxuhao/spring-framework.
Comment From: caoxuhao
Thanks for your advise. I got it. I'm so sorry for my behavior. It's my first time pulling the Pr.
Comment From: snicoll
Attempting to create a CGLIB proxy from a final class should fail as follows:
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class org.springframework.aop.framework.DefaultAopProxyFactoryTests$FinalTarget: Common causes of this problem include using a final class or a non-visible class
at org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:227)
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:155)
at org.springframework.aop.framework.DefaultAopProxyFactoryTests.createProxyOnFinalClass(DefaultAopProxyFactoryTests.java:36)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class org.springframework.aop.framework.DefaultAopProxyFactoryTests$FinalTarget
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:653)
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:26)
at org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy.generate(ClassLoaderAwareGeneratorStrategy.java:39)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:366)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:575)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.lambda$new$1(AbstractClassGenerator.java:107)
at org.springframework.cglib.core.internal.LoadingCache.lambda$createEntry$1(LoadingCache.java:52)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:57)
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:130)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:317)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:562)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:407)
at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:62)
at org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:218)
... 5 more
This changes makes it so that the exception is no longer thrown as it should so this isn't an optimization but a breaking change. Attempting to create the proxy on a final class should fail so I am going to close this PR.