I updated our application from Spring 5.3.30 to Spring 6.1.2 and Java version from 8 to 21. I also updated bunch of other libraries.

Now when I am trying to run the application, I am facing following exception.

Caused by: org.springframework.aop.framework.AopConfigException: Unexpected AOP exception
    at org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:233)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:160)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.buildProxy(AbstractAutoProxyCreator.java:517)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:464)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:369)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:318)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:437)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1776)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599)
    ... 66 more
Caused by: java.lang.ClassCastException: class com.somepackage.MailConfigProperties$$SpringCGLIB$$0 cannot be cast to class org.springframework.cglib.proxy.Factory (com.somepackage.MailConfigProperties$$SpringCGLIB$$0 and org.springframework.cglib.proxy.Factory are in unnamed module of loader 'app')
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:91)
    at org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:218)
    ... 75 more

Might be related: https://stackoverflow.com/questions/76824135/getting-springcglib0-cannot-be-cast-to-class-org-springframework-cglib-proxy#comment137004443_76824135

https://stackoverflow.com/questions/76381260/classcastexception-for-configuration-cglib-proxy-and-org-springframework-cglib-p

Comment From: snicoll

@sagarrohat thanks for the report but it's impossible for us to know what could be the issue by just looking at a stacktrace. Please create a small application that we can use to reproduce the problem. You can share it with us by attaching a zip to this issue or pushing the code to a GitHub repository.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: sagarrohat

@snicoll I was not able to reproduce the issue with small application, because I had a multi-module maven project with a lot of dependencies. My best guess was that it is because of different versions of dependencies (hibernate, flyway, data-rest, jpa, Apache commons)

So instead of updating to Spring 6, I migrated the application to Spring boot 3.x, which seems to resolve the issue.

Comment From: sbrannen

Glad to hear you resolved the issue, @sagarrohat, and thanks for providing that feedback

Comment From: snicoll

@sagarrohat if the move to Spring Boot fixed the issue for you, I am afraid that this issue is no longer actionable. I suspect that the proxy policy (JDK proxy vs. cglib) can be one of the reasons. Spring Boot does opt-in to cglib by default, framework does not.

Comment From: kubapet

I have the same issue, I m trying to investigate the cause in a big monolith so it is a pain. But is so funny to read this. You know, it is typical Spring. "it's impossible for us to know what could be the issue by just looking at a stacktrace." - It should be possible in normal apps lol. A Stacktrace + Reasonable error message and you should know what and where. But in Spring you typically have to reverse-engineer half of the framework and read through out the entire stack overflow to figure out what is wrong :)

Comment From: Adityakhare29

I have the same issue, I m trying to investigate the cause in a big monolith so it is a pain. But is so funny to read this. You know, it is typical Spring. "it's impossible for us to know what could be the issue by just looking at a stacktrace." - It should be possible in normal apps lol. A Stacktrace + Reasonable error message and you should know what and where. But in Spring you typically have to reverse-engineer half of the framework and read through out the entire stack overflow to figure out what is wrong :)

Bro what is the fix