0

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 !

Som
  • 1,522
  • 1
  • 15
  • 48
  • Does this answer your question? [How to make spring inject value into a static field](https://stackoverflow.com/questions/11324372/how-to-make-spring-inject-value-into-a-static-field) – Thiyagu Nov 29 '21 at 15:25
  • Nope.. Its not a spring project. – Som Nov 29 '21 at 15:50
  • What framework do you use for dependency injection? – talex Nov 29 '21 at 15:55
  • @talex : I am using java play framework. I generally do dependency injection by the constructor. As I dont have a constructor to this util class, I am facing the issue. – Som Nov 29 '21 at 16:03
  • Usually in such case you need to convert your class to service. – talex Nov 29 '21 at 16:05
  • Can you add [playframework](https://stackoverflow.com/tags/playframework/info) tag as well? – Thiyagu Nov 29 '21 at 16:30
  • *Is this a valid way to perform dependency injection* - Did you try if your approach injects the dependencies? Looks like it seems to work, but you want to ensure this is the right approach. Is that right? – Thiyagu Nov 29 '21 at 16:32
  • My $0.02 - I have worked with a class with static methods and dependency injection (using the same approach in the linked post by using a setter. But that was for spring). Later on, it caused a few troubles (it being a static) as we evolved the code. I don't remember the specifics though. – Thiyagu Nov 29 '21 at 16:36
  • 1
    Related: https://stackoverflow.com/questions/55213803/use-dependency-injection-in-static-class – Steven Nov 29 '21 at 16:50
  • 2
    this is an awkward thing to do because the DI container wants to be in charge of when it creates things, but for static the class loader is in charge. you can also see this as lying about your object's dependencies, using static implies you don't have dependencies. I use static methods only for things that have no dependencies. better off to not use static here. since this is play maybe partial application is an option? – Nathan Hughes Nov 29 '21 at 16:50
  • 3
    @NathanHughes: Considering that the dependency is required in the static methods, it basically means that the class *is used as if* it is a static class, even though it is an instance class. I would give the exact same answer to this question as I did to that referenced question. – Steven Nov 29 '21 at 16:55
  • 2
    Warning: Injecting dependencies into static fields leads to the [Temporal Coupling anti-pattern](https://mng.bz/4O7v). – Steven Nov 29 '21 at 16:58
  • C# and Java aren't that different and the same DI principes apply – Steven Nov 29 '21 at 18:24
  • @Steven : I have created a constructor for my Util class to inject the dependencies required. But please tell me does that stand for a good design. I am a bit skeptical. My junits looks good now. Hopefully the container testing in dev works also ! Then I can go ahead with the changes. – Som Nov 29 '21 at 20:09
  • @NathanHughes : Yup I agree with your DI and static class loader conflict. That's why I am a bit of less confident. I am using a constructor to inject the dependencies now. But does it invalidates the usefulness and concept of an Util class ? – Som Nov 29 '21 at 20:12
  • One more option I can see is that pass that `myDBCache` and `myConfig` objects as the method parameters of the util class static method and initialize them from the caller method of the Util class. In that way I dont have to make my Util class look dirty. – Som Nov 29 '21 at 20:49
  • @Som: It's hard to comment on your design as you never shared the real code. From a DI perspective, however, constructor injection is mostly the best answer (but be aware of constructor over-injection). To better understand what to inject, and what not, I'd suggest reading this [freely available first chapter](https://livebook.manning.com/book/dependency-injection-principles-practices-patterns/chapter-1) of [DIPP&P](https://cuttingedge.it/book/). – Steven Nov 30 '21 at 08:04

0 Answers0