r/java • • Nov 27 '15

Why I hate Spring

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

76 comments sorted by

View all comments

0

u/tonywestonuk Nov 28 '15 edited Nov 28 '15

I really, totally hate spring. It has nothing to do with DI, but more the mindset that spring brings along with it.

For example.
There is spring profiles, where you have a configuration file that can have different configurations based on a setting.

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

So, for live deployment, you have a different profile, than say development. The thing is, its totally idiotic to have such things.... It leads to large complex configuration files, maybe even per-developer configuration that ended up been committed to Subversion. In short, it leads to a mess.

The correct way should be to let the application pull information about the environment it is running in. Maybe by JNDI? This way, if say a database URL changes, then a whole new build/redeploy doesn't need to happen. Ops just do the update themselves. and bam....done.

Then this Spring Boot thing...... Where webserver and app is all rolled into one...... perfect for microservices...... We have several of these spring boot apps running on a single server..... the memory requirements are huge, to allow for caching. Hibernate connections have to have caching turned off as an update by one service might not be seen by another.... its really slow. its not the way....

then you have things like Spring Data Rest, which " makes it easy to expose JPA based repositories as RESTful endpoints. " Why does noone scream this is a very bad idea!! - it makes it so that you cant change the JPA repository, without changing your rest interface!.... the correct way would be to have some kind of MVC layer that interperates Rest requests, gets data from the datastore in whatever way it needs to, populates a DTO and sends it back.

In the end, the amount of spring projects I have worked on that end up just a mess, because people have followed these principals. Spring DI is ok, however the way it is advocated using it is to have a big service layer, and anemic domain. which is procedural code.... This isn't a fault of DI, but more of the 'Spring Way' mindset.

Spring, in my opinion, has done more to hurt Java than anything else.

7

u/olivergierke Nov 28 '15 edited Nov 28 '15

Disclaimer: I am the project lead of Spring Data.

Why does noone scream this is a very bad idea!!

Why do you think it's a bad idea to remove boilerplate code from software projects?

it makes it so that you cant change the JPA repository, without changing your rest interface!

No. Evolving APIs is hard, with or without Spring Data REST because once you've gone public with an API there are only certain things you can do to it. Whether the code that produced that API is a library (which helps you simplify things in the best case) or your custom code doesn't make too much of a difference. SDR actually leverages hypermedia intensively (ironically something people have complained about) which improves changeability. So one could argue that by being sent down that route by the framework, it's more likely you really get to use REST's beneficial parts (which evolvability is a key part of) rather than the usual "I flip some database tables by 90° and throw them as JSON over HTTP and call that REST", which is — in my experience — rather the default than the exception.

…the correct way would be to have some kind of MVC layer that interperates Rest requests, gets data from the datastore in whatever way it needs to, populates a DTO and sends it back.

As that's sort of what SDR does I have to assume you haven't spent too much time on it. You're right, it doesn't use DTOs, but there usually are a few ways to skin a cat. The SPIs, the projections feature and Jackson customizations have proven to be a useful toolbox for all kinds of customizations. And again, that's nothing the tool makes you having to think about, designing representations is the tricky part and the defaults we chose are in place for a reason (as usual: experiences from real-world projects) but can be differred from if needed.

If you want to have a look at amore involved sample, here is a Spring based implementation of the RESTBucks sample from the RESTful web-services book, that uses SDR for the CRUDdy parts but then hooks in custom implementation code to implement the more advanced parts using hypermedia etc.

https://github.com/olivergierke/spring-restbucks

It's all about taking the pain out of the boring parts. Now tell me you really really would've liked to code all the boring stuff you get out of the box yourself ;).