Skip to content

Spring AOP cannot generate proxy for lambda on Java 16+ #27971

Closed
@Ferioney

Description

@Ferioney

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions