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

Missing reflection hint for custom PropertySourceFactory in native image #30175

Closed
andrey-nakin opened this issue Mar 23, 2023 · 1 comment
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Milestone

Comments

@andrey-nakin
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 23, 2023
@sbrannen sbrannen changed the title NoSuchMethodException when using @PropertySource with a custom factory in Spring Native application NoSuchMethodException when using @PropertySource with a custom factory in native image Mar 23, 2023
@sbrannen sbrannen added the theme: aot An issue related to Ahead-of-time processing label Mar 23, 2023
@sbrannen sbrannen self-assigned this Mar 23, 2023
@sbrannen sbrannen added type: enhancement A general enhancement status: waiting-for-triage An issue we've not yet triaged or decided on in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 23, 2023
@sbrannen sbrannen changed the title NoSuchMethodException when using @PropertySource with a custom factory in native image Missing reflection hint for custom PropertySourceFactory in native image Mar 23, 2023
@sbrannen sbrannen removed their assignment Apr 4, 2023
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement labels Apr 25, 2023
@snicoll snicoll self-assigned this Apr 25, 2023
@snicoll snicoll added this to the 6.0.x milestone Apr 25, 2023
@snicoll
Copy link
Member

snicoll commented Apr 25, 2023

Thanks for the sample @andrey-nakin. While fixing this, I've found another issue that I am now looking at, see #30376

@snicoll snicoll modified the milestones: 6.0.x, 6.0.9 Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants