Closed
Description
I have a custom YAML resource that should be mapped to a Java property class. I used to do this using the following approach:
@Component
@ConfigurationProperties("sample-data")
@PropertySource(
value = "classpath:sample-data.yaml",
factory = YamlPropertySourceFactory.class)
public class SampleDataProperties {
...
where YamlPropertySourceFactory
is pretty simple:
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable final String name, final EncodedResource encodedResource) {
final var factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
final var properties = factory.getObject();
return new PropertiesPropertySource(Objects.requireNonNull(encodedResource.getResource().getFilename()),
Objects.requireNonNull(properties));
}
}
Unfortunately my custom factory causes NoSuchMethodException
when I build my application into a Spring Native image and then run it.
2023-03-23T09:49:07.586Z ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to instantiate class com.example.demo.config.YamlPropertySourceFactory
at org.springframework.core.io.support.PropertySourceProcessor.instantiateClass(PropertySourceProcessor.java:146) ~[na:na]
at org.springframework.core.io.support.PropertySourceProcessor.processPropertySource(PropertySourceProcessor.java:81) ~[na:na]
at com.example.demo.DemoApplication__ApplicationContextInitializer.processPropertySources(DemoApplication__ApplicationContextInitializer.java:46) ~[com.example.demo.DemoApplication:na]
at com.example.demo.DemoApplication__ApplicationContextInitializer.initialize(DemoApplication__ApplicationContextInitializer.java:33) ~[com.example.demo.DemoApplication:na]
at com.example.demo.DemoApplication__ApplicationContextInitializer.initialize(DemoApplication__ApplicationContextInitializer.java:27) ~[com.example.demo.DemoApplication:na]
at org.springframework.context.aot.AotApplicationContextInitializer.initialize(AotApplicationContextInitializer.java:72) ~[com.example.demo.DemoApplication:6.0.6]
at org.springframework.context.aot.AotApplicationContextInitializer.lambda$forInitializerClasses$0(AotApplicationContextInitializer.java:61) ~[com.example.demo.DemoApplication:6.0.6]
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:605) ~[com.example.demo.DemoApplication:3.0.4]
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:385) ~[com.example.demo.DemoApplication:3.0.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:309) ~[com.example.demo.DemoApplication:3.0.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[com.example.demo.DemoApplication:3.0.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[com.example.demo.DemoApplication:3.0.4]
at com.example.demo.DemoApplication.main(DemoApplication.java:10) ~[com.example.demo.DemoApplication:na]
Caused by: java.lang.NoSuchMethodException: com.example.demo.config.YamlPropertySourceFactory.<init>()
at java.base@17.0.6/java.lang.Class.getConstructor0(DynamicHub.java:3585) ~[com.example.demo.DemoApplication:na]
at java.base@17.0.6/java.lang.Class.getDeclaredConstructor(DynamicHub.java:2754) ~[com.example.demo.DemoApplication:na]
at org.springframework.core.io.support.PropertySourceProcessor.instantiateClass(PropertySourceProcessor.java:141) ~[na:na]
... 12 common frames omitted
Please refer to spring-native-issues repo with a complete demo. Please make sure you're using property-source
branch of the repo.