Over a million developers have joined DZone.

Dependency Injection and Type Safety

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

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?

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}