spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 356 forks source link

Error : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectingArgumentResolverBeanPostProcessor': Initialization of bean failed; nested exception is com.oracle.svm.core.jdk.UnsupportedFeatureError: Proxy class defined by interfaces [interface org.springframework.beans.factory.config.BeanPostProcessor #1705

Closed tushar256 closed 2 years ago

tushar256 commented 2 years ago

Hello,

I am facing the following error when running spring boot native application:

Compiles successfully mvn -Pnative -DskipTests -Dactive.profile=UAT clean package

Error while running target/hello-world

2022-08-25 14:43:50.351  INFO [hello-world,,] 885 --- [           main] o.s.nativex.NativeListener               : AOT mode enabled

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.1)

2022-08-25 14:43:50.464  INFO [hello-world,,] 885 --- [           main] com.test.MicroserviceApplication         : Starting MicroserviceApplication v1.0-SNAPSHOT using Java 17.0.4 on BAGBX0NA5J with PID 885 (/mnt/c/demo-projects/hello-world/target/hello-world started by otia in /mnt/c/demo-projects/hello-world)
2022-08-25 14:43:50.465  INFO [hello-world,,] 885 --- [           main] com.test.MicroserviceApplication         : The following 1 profile is active: "UAT"
2022-08-25 14:43:50.714  WARN [hello-world,,] 885 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectingArgumentResolverBeanPostProcessor': Initialization of bean failed; nested exception is com.oracle.svm.core.jdk.UnsupportedFeatureError: Proxy class defined by interfaces [interface org.springframework.beans.factory.config.BeanPostProcessor, interface org.springframework.beans.factory.BeanFactoryAware, interface org.springframework.beans.factory.BeanClassLoaderAware] not found. Generating proxy classes at runtime is not supported. Proxy classes need to be defined at image build time by specifying the list of interfaces that they implement. To define proxy classes use -H:DynamicProxyConfigurationFiles=<comma-separated-config-files> and -H:DynamicProxyConfigurationResources=<comma-separated-config-resources> options.
2022-08-25 14:43:50.723 ERROR [hello-world,,] 885 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectingArgumentResolverBeanPostProcessor': Initialization of bean failed; nested exception is com.oracle.svm.core.jdk.UnsupportedFeatureError: Proxy class defined by interfaces [interface org.springframework.beans.factory.config.BeanPostProcessor, interface org.springframework.beans.factory.BeanFactoryAware, interface org.springframework.beans.factory.BeanClassLoaderAware] not found. Generating proxy classes at runtime is not supported. Proxy classes need to be defined at image build time by specifying the list of interfaces that they implement. To define proxy classes use -H:DynamicProxyConfigurationFiles=<comma-separated-config-files> and -H:DynamicProxyConfigurationResources=<comma-separated-config-resources> options.
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:628)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:270)
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:762)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:567)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
        at com.test.MicroserviceApplication.main(MicroserviceApplication.java:57)
Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: Proxy class defined by interfaces [interface org.springframework.beans.factory.config.BeanPostProcessor, interface org.springframework.beans.factory.BeanFactoryAware, interface org.springframework.beans.factory.BeanClassLoaderAware] not found. Generating proxy classes at runtime is not supported. Proxy classes need to be defined at image build time by specifying the list of interfaces that they implement. To define proxy classes use -H:DynamicProxyConfigurationFiles=<comma-separated-config-files> and -H:DynamicProxyConfigurationResources=<comma-separated-config-resources> options.
        at com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:89)
        at com.oracle.svm.reflect.proxy.DynamicProxySupport.getProxyClass(DynamicProxySupport.java:158)
        at java.lang.reflect.Proxy.getProxyConstructor(Proxy.java:48)
        at java.lang.reflect.Proxy.getProxyClass(Proxy.java:398)
        at org.springframework.util.ClassUtils.createCompositeInterface(ClassUtils.java:784)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.getTargetShadowMatch(AspectJExpressionPointcut.java:437)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:295)
        at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:251)
        at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:289)
        at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:321)
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:128)
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:97)
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:78)
        at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:341)
        at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:455)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1808)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
        ... 15 common frames omitted

My environment is: GraalVM CE 22.2.0 (build 17.0.4+8-jvmci-22.2-b06) Open JDK 17.0.4 2022-07-19 Spring Boot: 2.7.1 spring-aot-maven-plugin: 0.12.1

After compiling ( mvn -Pnative -DskipTests -Dactive.profile=UAT clean package ) when I run my program, I get the above error.

Please find my minimal code sample here : https://github.com/tushar256/hello-world

Thanks

sdeleuze commented 2 years ago

You should be able to fix that by adding proxy hints like @JdkProxyHint(types= { BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class }). See related documentation in https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#native-hints. Spring Boot 3 native support should be able to infer such hint automatically.

tushar256 commented 2 years ago

Hello I am still getting the same error message after adding this @JdkProxyHint(types= { BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class, ApplicationContextAware.class, EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class}) on Main Class ( annotated with @SpringBootApplication )

Complete Main Class:

`@JdkProxyHint(types= { BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class, ApplicationContextAware.class, EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class}) @NativeHint( types = { @TypeHint(types = {BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class, ApplicationContextAware.class, EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class}, access = {TypeAccess.PUBLIC_CONSTRUCTORS, PUBLIC_METHODS}), @TypeHint(types = {ConsoleAppender.class, SizeAndTimeBasedRollingPolicy.class, PatternLayout.class, RollingFileAppender.class, PatternLayoutEncoder.class}, access = {TypeAccess.PUBLIC_CONSTRUCTORS, PUBLIC_METHODS}) }) @AotProxyHints( { @AotProxyHint(interfaces ={BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class, ApplicationContextAware.class, EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class}), @AotProxyHint(targetClass = DefaultController.class, proxyFeatures = ProxyBits.IS_STATIC), @AotProxyHint(targetClass= KafkaReceiver.class, proxyFeatures = ProxyBits.IS_STATIC), @AotProxyHint(targetClass= KafkaSender.class, proxyFeatures = ProxyBits.IS_STATIC) } )

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) public class MicroserviceApplication {

public static void main(String[] args) {
    SpringApplication.run(MicroserviceApplication.class, args);
}

} `

Please find my sample code here: https://github.com/tushar256/hello-world

sdeleuze commented 2 years ago

I tried your repro from https://github.com/tushar256/hello-world and it works with:

@NativeHint(
        types = {
                @TypeHint(types = {BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class, ApplicationContextAware.class, EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class},
                        access = {TypeAccess.PUBLIC_CONSTRUCTORS, PUBLIC_METHODS}),
                @TypeHint(types = {ConsoleAppender.class, SizeAndTimeBasedRollingPolicy.class, PatternLayout.class, RollingFileAppender.class, PatternLayoutEncoder.class},
                            access = {TypeAccess.PUBLIC_CONSTRUCTORS, PUBLIC_METHODS})
        }, jdkProxies = {
        @JdkProxyHint(types= { BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class }),
        @JdkProxyHint(types= { ApplicationContextAware.class, EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class }),
        @JdkProxyHint(types= { java.util.List.class, java.util.RandomAccess.class, java.lang.Cloneable.class, java.io.Serializable.class, java.util.Collection.class })
        })
@AotProxyHints(  { @AotProxyHint(interfaces ={BeanPostProcessor.class, BeanFactoryAware.class, BeanClassLoaderAware.class, ApplicationContextAware.class,
                    EnvironmentCapable.class, EnvironmentAware.class, Servlet.class, ServletConfig.class, Serializable.class}),
                    @AotProxyHint(targetClass = DefaultController.class, proxyFeatures = ProxyBits.IS_STATIC),
                   @AotProxyHint(targetClass= KafkaReceiver.class, proxyFeatures = ProxyBits.IS_STATIC),
                   @AotProxyHint(targetClass= KafkaSender.class, proxyFeatures = ProxyBits.IS_STATIC) } )

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

after removing -H:DynamicProxyConfigurationFiles=/mnt/c/git/hello-world/target/classes/META-INF/native-image/proxy-config.json whihc is useless since discovered automatically.

tushar256 commented 2 years ago

Thank you @sdeleuze it worked!!