Guice best practices and anti-patterns

本秂侑毒 提交于 2019-12-09 07:27:28

问题


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 patterns to this question.


回答1:


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;
    }
}



回答2:


There are some on the Guice project page. Here's a link to the first one, and you can see the others in the sidebar.

That said, I think it would be great to see others posted and voted on here. Then we can write the best of them up for the Guice pages.



来源:https://stackoverflow.com/questions/1683777/guice-best-practices-and-anti-patterns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!