How to avoid having injector.createInstance() all over the place when using guice?

前端 未结 3 2031
故里飘歌
故里飘歌 2020-12-30 05:28

There\'s something I just don\'t get about guice: According to what I\'ve read so far, I\'m supposed to use the Injector only in my bootstrapping class (in a standalone appl

3条回答
  •  忘掉有多难
    2020-12-30 05:42

    To extend on the answer Gamlor posted, you need to also differentiate between the object types you are using.

    For services, injection is the correct solution, however, don't try to always make data objects (which are generally the leafs in your object graph) injectable. There may be situations where that is the correct solution, but injecting a Provider is probably not a good idea. A colleague of mine ended up do that, it made the code base very confusing after a while. We just finished cleaning it all out and the Guice modules are much more specific now.

提交回复
热议问题