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 when using @PropertySource with a custom factory #253

Closed
andrey-nakin opened this issue Mar 23, 2023 · 2 comments
Closed
Labels
invalid This doesn't seem right

Comments

@andrey-nakin
Copy link

andrey-nakin commented Mar 23, 2023

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 [email protected]/java.lang.Class.getConstructor0(DynamicHub.java:3585) ~[com.example.demo.DemoApplication:na]
	at [email protected]/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.

@andrey-nakin andrey-nakin added the bug Something isn't working label Mar 23, 2023
@bclozel
Copy link
Collaborator

bclozel commented Mar 23, 2023

I believe this is a Spring Framework problem, could you create a new in the relevant issue tracker please?

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2023
@bclozel bclozel added invalid This doesn't seem right and removed bug Something isn't working labels Mar 23, 2023
@andrey-nakin
Copy link
Author

@bclozel reposted to spring-framework, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants