Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice best practices and anti-patterns

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

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.