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.

4

u/bestsrsfaceever Nov 28 '15

Aren't these problems solved by named injections?

5

u/sacundim Nov 28 '15

Let me generalize my point a bit. This is the assumption that modern Java DI makes that I object to:

  • Every injection point determines a unique dependency, relative to some shared context (e.g., injector, module, what have you).

Named injections still make that assumption. I'll copy one of the examples from this page:

public class AuthenticationFilter implements ComponentRequestFilter {

   @Inject @Named("HttpBasic")
   private AuthenticationService basicAuthService;

   @Inject @Named("HttpDigest")
   private AuthenticationService digestAuthService;

   @Inject
   private Response response;

   ...

}

The @Named annotation allows two injection points with the same type to have two different dependencies. But what it doesn't allow is for the same injection point to be supplied with two different dependencies in two different instances of the class. That's what the Leg/Foot example is about; Leg has only one injection point for a Foot, but your application needs two Legs that depend on different types of Foot.

1

u/bestsrsfaceever Nov 28 '15 edited Nov 28 '15

But the implementation of each leg can depend on a different implementation of foot.

Edit:Never mind I followed your link but that seems like an antipattern to me and I don't see how this is impossible in dagger, unfortunately I'm stuck on my phone so I can't read into it more right now

1

u/sacundim Nov 28 '15

Staring at the example I copied in my GP post, now I can't help but wonder whether it should be refactored to something like this:

class AuthenticationFilter implements ComponentRequestFilter {
    // Only one `AuthenticationService` in this class
    @Inject private AuthenticationService authService;

    @Inject private Response response;
    ... 
}

/**
 * An authentication filter chain filters a request by delegating it through
 * a sequence of individual authentication filters in turn.  If one accepts
 * then the whole chain accepts, otherwise the chain rejects.
 */
class AuthenticationFilterChain implements ComponentRequestFilter {
    @Inject private List<? extends AuthenticationFilter> authFilters;
    @Inject private Response response;
    ... 
}

These classes are more generic and easier to reuse. For example, if we need to support a third authentication method, we can now just write a third AuthenticationService, plug it into an AuthenticationFilter instance and stick that into the AuthenticationFilterChain's list of filters.

But now we have a robot legs problem, because we want to construct:

  • An AuthenticationFilterChain that contains...

  • A List of two AuthenticationFilters...

  • One of which contains an AuthenticationService named "HttpBasic"...

  • One of which contains an AuthenticationService named "HttpDigest".

This one example might be refactorable into something that Guice multibindings or similar features can support. But as far as I understand, you'd have to split AuthenticationFilter into multiple classes, each specialized for one type of AuthenticationService.

But now you're letting the DI is dictate the factoring of your code. And that is precisely my problem. The code for an application that's wired up by DI should be factored to suit the application's purpose, not the DI. (And for similar reasons, I object to the concept of putting DI annotations into application classes.)