0

In my spring boot project when I am trying to inject values using @Value annotation its giving inconsistent behavior. In my controller it's working fine but when I use it in my @Component class the field is null. I tried using @PropertySource annotation on my component class but it still didn't work. I am using spring boot 3.1.0 and Java 17. Please advise.

Sharing repository link of the project. Contact Management Project

In this project I am trying to read the algorithm property value from properties file in my src/main/java/com/shivam/cms/utils/EncryptionUtil.java but it fails. Same works in ContactContoller.java file

Shivam...
  • 409
  • 1
  • 8
  • 21
  • 1
    Using `@Value` in `@Component` annotated classes should work fine. Can you give a code example where it doesn't work? – g00glen00b May 26 '23 at 09:25
  • Hello I have shared my code repository please check. – Shivam... May 26 '23 at 09:52
  • I was just trying to print the value in constructor , i have no need for constructor injection. It was still null when i wasnt using constructor injection, so that question doesn't answer my query. – Shivam... May 26 '23 at 10:14
  • I have done my part of stackoverflow search but could not resolve it. – Shivam... May 26 '23 at 10:14
  • @Shivam... Can you add the relevant code of the `EncryptionUtil` to your question? And can you also give an example of how you verify that the property is `null`? – g00glen00b May 26 '23 at 10:15
  • 2
    Did you read the answer there? "As far I remember, the value injection occurs after the constructor call" – Mar-Z May 26 '23 at 10:15
  • Well i tried it again now without constructor injection and it seems to work now, I was stuck on it since yesterday and it wasn't working. My apologies @Mar-Z you were right. Thanks guys. – Shivam... May 26 '23 at 10:39

0 Answers0