I have a class which has few static utility function.
I want to inject a property value << which is a static field>> without creating its bean.
@Component
class TestUtils {
@Value("${toke.value}")
public static String token;
public static String doOperation(String value) {
.... do some operation using toke
return result;
}
public static void setToken(String token ) {
TestUtils.token = token;
}
}
I am never creating object of this class. The method is called
TestUtils.doOperation(parms);
Just want to know is there is any way in which i set the property of this value , when application starts.
Thanks.
Just don’t. Make your methods instance methods instead of static methods. Make your fields private instance fields instead of public static fields, and inject an instance of the bean where you need access to call the method. That’s the whole point of dependency injection.
Spring beans are singletons by default, so there won’t be more than one such field in the JVM anyway. And making it an instance method and an injectable component will make the code using it testable, which is not the case with a static method.