I have the following utility class with some static methods and static fields.
Now I need to add a dependency into this class. This is how I am trying to do :
public class MyUtil {
private static final List<String> status = Arrays.asList(new String[] { MyStatus.SUCCESS, MyStatus.FAILURE });
@Inject
public static MyDBCache myDBCache; // newly added field
@Inject
public static Configuration myConfig; // newly added field
public static Boolean somePublicMethod() {
// some code here
// myConfig --> will read value from conf file.
// myDBCache will call my cache configured to read some value.
}
private static String somePrivateMethod() {
// some code here
}
}
My question is : Is this a valid way to perform dependency injection and will this ensure this will actually create the dependency object at runtime. I know I can use @Inject
to a field which is non-static and , and I can inject the dependency via the constructor. But as this is a simple utility class with no explicit constructor, I ama little confused how to progress. And also this is not a spring boot project.
Please help !