35

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.

Community
  • 1
  • 1
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • I'd encourage anyone interested in contributing to this question to check out the [Guice docs](http://stackoverflow.com/documentation/guice/topics) topics instead. – dimo414 Feb 08 '17 at 00:35

2 Answers2

31

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;
    }
}
SamBeran
  • 1,944
  • 2
  • 17
  • 24
14

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.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87