spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.59k stars 38.13k forks source link

Java 17, aop and hibernate class issue of named module class type error #33787

Open johnxu2013 opened 1 week ago

johnxu2013 commented 1 week ago

In Java 17 and spring mvc application, with xml configuration files, I called org.hibernate.query.Query in aop, getting the issue: jdk.proxy2.$Proxy155 is in module jdk.proxy2 of loader 'app'; org.hibernate.query.Query is in unnamed module of loader 'app'

Caused by: java.lang.ClassCastException: class jdk.proxy2.$Proxy155 cannot be cast to class org.hibernate.query.Query (jdk.proxy2.$Proxy155 is in module jdk.proxy2 of loader 'app'; org.hibernate.query.Query is in unnamed module of loader 'app') at org.hx.resequence.batch.job.utils.HibernateQueryLoggingAspect.logSqlAndParameters(HibernateQueryLoggingAspect.java:22) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89) at org.hx.resequence.batch.job.utils.HibernateExceptionLoggingAspect.logHibernateException(HibernateExceptionLoggingAspect.java:32) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Similar issue also happens when I defined an interceptor, but it cannot have the correct type, log as below: Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'Intercerptor' is expected to be of type 'org.HibernateSQLInterceptor' but was actually of type 'jdk.proxy2.$Proxy197' at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1401) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ... 239 common frames omitted

johnxu2013 commented 1 week ago

I searched web, and it said this is because JEP 396, the modules are different in java 17. But how to resolve it in aop?

sdeleuze commented 1 week ago

Looks like potentially a question [StackOverfow](for https://stackoverflow.com/questions/tagged/spring), if that's the case please close this issue and ask it on SO. If you think that's a genuine bug, please provide a self-contained reproducer (link to a repository or attached archive), and please make sure you provide the documentation to reproduce step by step.

spring-projects-issues commented 5 days ago

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.