Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchMethodException exception when using ConfigurationProperties #28

Open
chrisjs opened this issue Aug 12, 2019 · 1 comment
Open

Comments

@chrisjs
Copy link
Contributor

chrisjs commented Aug 12, 2019

using GraalVM CE 19.0.2

when using ConfigurationProperties, compilation will succeed but an error is thrown at runtime. this can be reproduced by applying the below patch against the commandLinerunner sample:

diff --git a/samples/commandlinerunner/clr b/samples/commandlinerunner/clr
new file mode 100755
index 0000000..8c1b419
Binary files /dev/null and b/samples/commandlinerunner/clr differ
diff --git a/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/CommandlinerunnerApplication.java b/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/CommandlinerunnerApplication.java
index 70f687e..fb342e1 100644
--- a/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/CommandlinerunnerApplication.java
+++ b/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/CommandlinerunnerApplication.java
@@ -3,9 +3,11 @@ package com.example.commandlinerunner;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 
 @SpringBootApplication(proxyBeanMethods=false)
+@EnableConfigurationProperties(PropTest.class)
 public class CommandlinerunnerApplication {
 
 //       private static final Logger LOGGER;
diff --git a/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/PropTest.java b/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/PropTest.java
new file mode 100644
index 0000000..f0a3e2f
--- /dev/null
+++ b/samples/commandlinerunner/src/main/java/com/example/commandlinerunner/PropTest.java
@@ -0,0 +1,16 @@
+package com.example.commandlinerunner;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@ConfigurationProperties("com.example")
+public class PropTest {
+       private String name;
+
+       public String getName() {
+               return name;
+       }
+
+       public void setName(String name) {
+               this.name = name;
+       }
+}

running the application yields the following:

SEVERE: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example-com.example.commandlinerunner.PropTest': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.commandlinerunner.PropTest]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.commandlinerunner.PropTest.<init>
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1307)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1201)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:866)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:404)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:319)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1275)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1263)
	at com.example.commandlinerunner.CommandlinerunnerApplication.main(CommandlinerunnerApplication.java:29)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.commandlinerunner.PropTest]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.commandlinerunner.PropTest.<init>
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1299)
	... 16 more
Caused by: java.lang.NoSuchMethodException: com.example.commandlinerunner.PropTest.<init>
	at java.lang.Class.getConstructor0(DynamicHub.java:3082)
	at java.lang.Class.getDeclaredConstructor(DynamicHub.java:2178)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
	... 17 more

@dsyer
Copy link
Contributor

dsyer commented Aug 16, 2019

We might eventually add a feature for user-provided configuration properties. Until then the workaround is easy: just add the com.example.commandlinerunner.PropTest to your META-INF/native-image/reflect-config.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants