Over a million developers have joined DZone.

Dependency Injection and Type Safety

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

I've had a couple interesting discussions recently about dependency injection and type safety. In particular there is a new article up discussing type safety in the Butterfly DI framework. I think Jakob makes some good points and argues well that perhaps too much is made of the lack of type safety in Spring XML config. (And yes, I know you don't have to do it in XML.)

Guice is perhaps the other end of the scale as the type safety of the configuration seems to be one of the guiding principles of Guice.

Personally, I find myself sympathetic to the benefits of static typing. I've worked on big systems (1M+ LOC) and have had these kinds of problems before; not with Spring in particular, but in general with code that references classes and methods by name rather than by a strong reference. Over the years, I've seen a number of little problems that cost me a tiny amount of time to find and correct - usually found quickly at runtime or in unit tests. And I've had big subtle problems a few times that cost me lots of time. Sometimes these are more subtle than just a bad class/method name and relate back to implicit typing of string vs numeric values or something like that.

To me, one of the things that Java is really good at it is static typing. If you're going to use Java to build your system, especially Java 5+ with generics and annotations, you should leverage the heck out of it. Otherwise, why are you using Java anyways? My feeling is that Guice was created in this spirit.

But I see all of these choice as valid options and to some degree a matter of preference on a continuum. Where do you fall? How much static typing do you like in your DI?

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}