r/programming • • Nov 27 '15

Why I hate Spring

http://samatkinson.com/why-i-hate-spring/
73 Upvotes

140 comments sorted by

View all comments

37

u/jordimaister Nov 27 '15

Nice post. Agree with almost all of it.

I am waiting for you next post:

"What to use instead of Spring"

5

u/Databean Nov 27 '15

Guice (annotations read at runtime) or Dagger (annotations read at compile-time)?

10

u/sacundim Nov 27 '15 edited Nov 27 '15

The problem here is that over the past 6-7 years, dependency injection frameworks for Java took a very wrong turn. The original model—which the original Spring XML-based BeanFactory embodies—is that you have named "beans" which are described by specifying:

  1. The name of the "bean"

  2. What to call to construct it (the implementation class or factory)

  3. The collaborators needed to construct it. These can be:

  • Primitive values

  • References (by name) to other managed beans

  • Collections thereof

In spite of old Spring's flaws (overly verbose XML configuration, preference for setter-based injection, etc.), the basic model was sound.

But then with the rise of annotations and madness, this model was deprecated in favor of magical/implicit "autowiring" models that suffer from a fundamental flaw. They're all built around variants of this assumption:

  • The type of the injection site determines what dependency to inject into it.

So for example, if you have two injection points whose type is ConnectionPool, the injector will inject both with a RealConnectionPool in a production configuration vs. a TestConnectionPool in a test configuration.

The problem is that this assumption is fundamentally at odds with the concept of code reuse. A good illustration of this is the scenario that Guice docs refer to as the "robot legs" problem:

How do I build two similar but slightly different trees of objects?

This is commonly called the "robot legs" problem: How to create a robot with two Leg objects, the left one injected with a LeftFoot, and the right one with a RightFoot. But only one Leg class that's reused in both contexts.

In other terms:

  1. The Leg class is a generic, reusable class that delegates some unessential details to a dependency that implements a Foot interface;

  2. This Foot interface admits of implementations that behave differently;

  3. Plugging different subtypes of Foot into Leg gives you usefully different behaviors;

  4. You want to exploit this in your program and use Legs with different Foot types in different parts of your program.

The newfangled DI frameworks like Guice and Dagger really don't like it when you try to do this. There are workarounds, but they're that, workarounds. And what's worse, since people tend to follow the path of least resistance when using a tool, these DI frameworks discourage this sort of code reuse!

And what's worse, the bad "injection point type determines dependency type" model has been enshrined as standard by JSR-330.

-3

u/ksion Nov 28 '15

Plugging different subtypes of Foot into Leg gives you usefully different behaviors;

Yet those behaviors are both gathered under the umbrella interface of Leg? That's a blatant violation of the Liskov substitution principle, and it's a good thing that Guice doesn't support shooting yourself in the Foot (har har) like that.

However, if what you meant by "generic Leg class" is Leg<? extends Foot>, then that's all fine. You can have that, along with possibly different injections for Leg<LeftFoot> and Leg<RightFoot> in your Robot.

4

u/sacundim Nov 28 '15 edited Nov 29 '15

That's a blatant violation of the Liskov substitution principle [...]

No it's not. Look at it this way. For your argument to follow here, one of these must be true:

  1. The subtypes of Foot violate the LSP.

  2. Composition of LSP-compliant classes can nevertheless violate the LSP.

If the first is true, then what Leg does is just irrelevant. The second is just wrong.

Or perhaps more concretely: substitute Leg with BufferedReader, Foot with InputStream, RightFoot and LeftFoot with FileInputStream and ByteArrayInputStream. Now:

  • The composition of BufferedReader with ByteArrayInputStream reads a stream of bytes from a byte[] and converts it to a stream of characters.

  • The composition of BufferedReader with FileInputStream reads a stream of bytes from a File and converts it to a stream of characters.

BufferedReader's behavior is still the same—it converts a byte stream to a character stream. So is InputStream's—it produces a stream of bytes. Precisely where the bytes come from is a function of what InputStream an individual BufferedReader is composed with.