question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring AOP cannot generate proxy for lambda on Java 16+

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sbrannencommented, Feb 4, 2022

I’m guessing that the JDK thinks that the com.example.issue.aopissue.service.IssueConfiguration$$Lambda$638/0x0000000800e796e0 type resides in same package as java.util.function.Supplier and therefore in the java.lang module instead of being local to your module, and I’m wondering if that’s perhaps a bug in the JDK.

For the sake of clarity, after investigating this issue, there does not appear to be an issue with the JDK. Rather:

  • Spring AOP is attempting to create a CGLIB-based proxy for the lambda expression.
  • You therefore need --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 to true.

1reaction
Ferioneycommented, Feb 9, 2022

@sbrannen checked and it worked as expected. Thank you!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found