Affects: Spring 5.3.2.RELEASE, Spring Boot 2.2.4.RELEASE
(I'm not sure if it's a Spring issue of Spring Boot issue.) I try to inject a resource from the classpath using the following syntax and everything works like a charm:
@Configuration
@RequiredArgsConstructor
public class MyConfiguration {
@Value("classpath:${my-file}")
private final Resource myFile;
// a lot of codes here;
}
But when I try to build the project (and run tests) in Docker, it fails with:
No qualifying bean of type 'org.springframework.core.io.Resource' available: expected at least 1...
I managed to resolve it by removing final
keyword from the field. But just wanted to know why it fails.
Thanks.
Comment From: chenqimiao
I guess this is because the property cannot be set for the final
field via reflection.
Comment From: akefirad
I'm not following. When it's final, it's constructor based injection, not field based. And as I said, with final, it works just fine on the host. It fails only in Docker.
Comment From: albertocavalcante
@akefirad Did you solve it?
Comment From: akefirad
Not really, I had to make the field non-final.
Comment From: amithkumarg
The reason you are getting this exception because in the lombok generated constructor, it didn't copy the @Value
annotation. For that you have to add the configuration (below file) in the project root folder. For multi module project, add it in the parent directory.
~lombok.config
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
lombok.copyableAnnotations += org.springframework.context.annotation.Lazy
Comment From: sbrannen
If you are still facing this issue, please provide a minimal sample application (preferably a GitHub project that we can clone or a ZIP file), so that we can observe the behavior.
Comment From: akefirad
It’s been almost 3 years ago, so I might be wrong. But I think we had the Lombok config you’re suggesting from the beginning in the project. I don’t have access to the project anymore. So feel free to close the issue.