I would like to ask a question as per title.
I have a util class such as follows:
@PropertySource("classpath:application.properties") public class ServiceUtils { @Value("${dummy}") public static String SOME_VAR; @Value("${dummy}") // Baeldung says that it's not possible to inject props to static vars public void setSomeVar(String var) { SOME_VAR = var; } }
When I launch the application, and debug, the above variable SOME_VAR
comes as null
🙁
I am aware that I am using this in a Util class, which is an antipattern, I think.
Could someone help me understand what I need to correct to get this to work?
Regards
Advertisement
Answer
Spring does not allow you to inject values in static variables. Use a non static instead.
If it has to be static, do something like this:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ServiceUtils { public static String SOME_VAR; @Value("${dummy}") public void setSomevar(String value) { SOME_VAR= value; } }