News for hackers. Stuff that matters.
Re:Spring Dependency Injection Styles – Why I love Java base... by cletus on Monday July 23, 2012 @10:23AM (#4281102)
I've used Spring on and off for 7+ years now. It was a seachange for Java and I like it a lot but the Java-based configuration has a serious defect.

Spring supports two methods of dependency injection: constructor injection and setter injection. Guice (in comparison) favours constructor injection. I prefer constructor injection too. It means objects can't be instatiated in an invalid state (assuming you enforce preconditions) whereas with setter injection you have to rely on lifecycle methods (eg @PostConstruct) to enforce that.

With Java-based Spring configuration you create instances rather than define dependencies in terms of classes (as Guice does it). This is a problem for constructor injection but not for setter injection.

This means when you create an instance to satisfy a dependency you need to explicitly supply all the constructor arguments. Spring would otherwise figure out setter injection.

With Guice you associate classes instead so you don't need to explicitly call a constructor with all the arguments. For Java-based configuration IMHO Guice is much better in this regard.

Also, Java-based Spring configuration relies on the cglib library, which may or may not be a problem in certain environments (eg its not officially supported on AppEngine but in my experience works fine for this).

So if you like constructor injection you have two options: component scanning or XML.

Component scanning is slow. Too slow. For even a small number of objects (<100) I've reduced startup times from ~30 seconds to ~10 seconds just by switching to explicit construction.

XML is really the best choice here. One thing you may not realize is you don't need to specify all the dependencies in XML (setter or constructor). You simply do a definition like:

    <bean class="com.example.Foo" />
and

    public class Foo {
      @Inject
      public class Foo(Bar bar, AnotherBar anotherBar) { ... }
    }
and Spring will figure it out.

IMHO this is far better than Spring's Java-based configuration.

EDIT: to clarify, @Inject here is JSR330 DI annotations [1] so java.named.Inject not com.google.inject.Inject. As that page notes Guice now supports either form.

[1]: http://code.google.com/p/google-guice/wiki/JSR330

Re:Spring Dependency Injection Styles – Why I love Java base... by tobiasflohre on Monday July 23, 2012 @04:43PM (#4282988)
"So if you like constructor injection you have two options: component scanning or XML." I totally disagree with this limitation. I do favor constructor injection and I am always using it with Java based configuration, I never use setter injection (maybe sometimes for optional dependencies). Why should it be a problem that you need an instance for satisfying a dependency? It's only a problem if you have circular dependencies, but then something is wrong with your design.

All the rest is flavor we could discuss about for hours without getting to an end, but Spring's Java based configuration definitely doesn't have a problem with constructor injection.

Regards, Tobias

Re:Spring Dependency Injection Styles – Why I love Java base... by stitchy on Monday July 23, 2012 @11:12AM (#4281347)
"So if you like constructor injection you have two options: component scanning or XML."

I only half agree that this is a limitation. I use as little component scanning as possible. I think it's better to scan for the configuration classes (hopefully there aren't many) and explicitly define the rest. I like this for two reasons, first for speed and second because everything is defined in a fairly central place. I can't tell you how many times I've been surprised by annotation based configuration.

Personally I like Spring's Java Configuration over Guice's. It's more flexible for my needs. Although I suppose that an argument could be made that that's the problem: configuration over convention.

I'm glad you mentioned the Guice style @inject is available with Spring now. I wasn't aware of that.

Re:Spring Dependency Injection Styles – Why I love Java base... by jsolson on Monday July 23, 2012 @11:32AM (#4281436)
I'm on the polar opposite end of this. We component scan everything we can, and I couldn't be happier. I have never once been surprised by what annotation based config assembled for me.

So, approaches may vary.