Spring AOP cannot generate proxy for lambda on Java 16+
See original GitHub issueI’m migrating to Java 17 and found an issue with proxy generation for ‘lambda beans’.
- Spring Boot: 2.6.3
- Maven: 3.8.4
- Java:
openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Corretto-17.0.2.8.1 (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.2.8.1 (build 17.0.2+8-LTS, mixed mode, sharing)
Spring AOP cannot generate a proxy for beans like:
@Bean
public Supplier<String> stringSupplier() {
return () -> "lambda supplier value";
}
Errors:
Caused by: org.springframework.aop.framework.AopConfigException:
Could not generate CGLIB subclass of class com.example.issue.aopissue.service.IssueConfiguration$$Lambda$638/0x0000000800e796e0:
Common causes of this problem include using a final class or a non-visible class;
nested exception is 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 @5ecddf8f
But if I use class implementation it works fine:
public class TestSupplierService implements Supplier<String> {
@Override
public String get() {
return "class supplier value";
}
}
I prepared demo project aop-issue to show the issue:
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Spring AOP proxies does not work with JavaFX - Stack Overflow
Here is the Main method, where I create the instance of the MyScene interface and wrap it with proxy. public class Main extends...
Read more >Build a custom Java runtime for AWS Lambda
The following blog post provides a walkthrough of how you can create and optimize a custom runtime for Java based Lambda functions.
Read more >Spring AOP Tutorial - YouTube
Aspect Oriented Programming in SpringAOP Jars : https://goo.gl/e23fxWSpring Video : https://goo.gl/CxnXpLSpring Full Course ...
Read more >Error - Can't Determine SAML Request - Google Groups
CglibAopProxy $DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) at org.apereo.cas.support.saml.web.idp.profile.sso.
Read more >[RTFACT-12462] Race condition when putting or updating ...
Upon running updating or creating permission target there is a race condition and ... invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.1.5.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For the sake of clarity, after investigating this issue, there does not appear to be an issue with the JDK. Rather:
--add-opens java.base/java.lang=ALL-UNNAMED
on Java 16 or higher in order to allow CGLIB to create the class-based proxy for the lambda expression.Note that
--add-opens java.base/java.lang=ALL-UNNAMED
is not necessary on Java 9 through Java 15.For the time being, people can use the
--add-opens
workaround.However, the Spring team will investigate a way to avoid the creation of CGLIB-based proxies for lambda expressions. A JDK dynamic proxy should always suffice for a class that can only ever be used via the functional interface it implements. Dynamic proxies should also be used for lambda expressions and method references even if
proxyTargetClass
has been set totrue
.@sbrannen checked and it worked as expected. Thank you!