Guice best practices and anti-patterns

后端 未结 2 604
独厮守ぢ
独厮守ぢ 2021-02-11 15:32

I\'m not sure if there is merit to this question or not, but are there any best practices and anti-patterns specific to Google Guice?

Please direct any generic DI patter

2条回答
  •  故里飘歌
    2021-02-11 16:07

    I have always felt that constructor injection to final fields is a best practice. It minimizes mutable state and makes the class easier to understand by making the class's formal dependencies explicit.

    public class MyClass {
        private final MyDependency dependency;
    
        @Inject
        public MyClass(MyDependency dependency) {
            this.dependency = dependency;
        }
    }
    

提交回复
热议问题