0

I have a Spring Boot Webflux app with:

@Value("classpath:file.json")
private Resource create;

The file is in resources/file.json, but create is always coming up null in the debugger.

I have also tried:

new ClassPathLoader("file.json") through manual loading and its working fine.

Any ideas why @Value can't resolve?

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • 1
    Are your sure the class containing this code is initialized as a Spring bean, either picked up by component scan or created manually? And where are you referencing the file also matters, it’ll be null in the constructor. If you’ve verified both to be not causing this issues, post a minimum, reproducible example. – Abhijit Sarkar Feb 09 '22 at 23:02
  • Did you try with absolute path as well? – Marcin Erbel Feb 09 '22 at 23:13
  • @AbhijitSarkar Yep. It's marked as Service. You are correct. It's working later on. I assumed it would initialized in the constructor. Thanks. – SledgeHammer Feb 10 '22 at 00:05

1 Answers1

0

Dependency injections happen after the initializers run on a class, that includes constructors. Dependencies are available as initialized within all instance methods as well as in a special lifecycle method marked @PostConstruct. Within a constructor, dependencies have their default values.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219