; last updated - 10 minutes read

In my business life, Spring and Spring Boot are my love-hate-relationship.

Spring - or the invention of simplicity

When I learned about Spring many years ago, it blew my mind. Such a simple and versatile framework. I converted within minutes. My boss did not, so it took a couple of years until I could use Spring in a real-life project.

Spring Blues

In the meantime, Spring had grown up. Or rather, it had reached the age of puberty. Gone was the simplicity of the early days. XML was the word of the day. Much of the program logic had been shifted to XML configurations. Mastering Spring had become a well-paid art, performed by high priests juggling with huge XML files. Watching them, everything looked easy, but writing a Spring program wasn't my cup of tea. I'm simple-minded, so I like my programs to be simple, too. In 2008, Spring was everything but simple.

Spring Boot: Spring gets simple again!


Spring History, compiled by QuickProgrammingTips at https://www.quickprogrammingtips.com/spring-boot/history-of-spring-framework-and-spring-boot.html, published under a CC BY 4.0 license
Image source: QuickProgrammingTips.com
Many people laughed at me. "Stop complaining," they said, "pouting gets you nowhere!". Even so, the Spring team listened to developers like me. They simplified Spring a lot. With Spring 4, they introduced annotations. A little time later, they introduced Spring Boot. That's a nice convention-over-configuration approach. It blew my mind within minutes. It looked awesome!

As a side remark, I had to convert our corporate application from JavaEE to Spring roughly in 2013. At the time, annotations were a mixed blessing. I knew you could do everything with annotations, but the XML-based documentation still dominated the Google index. Only when the annotation based configuration bubbled up the search index, casual (or rookie) developers started to benefit from it. Moral of the story: It's not enough to create great stuff. If you don't tell Google, nobody notices.

Since 2015, I didn't have the opportunity to use Spring Boot in a real-world project, so all I had were the opinions of my co-workers. More often than not, they told me that Spring Boot is great for starting a project, but the magic gets sour when the project grows. And every project grows.

Today, a friend asked me for an introduction to Spring development. That's an excellent excuse to have a look at the current state of the art. In particular, I'm interested in Spring Boot. How does it compete to JavaEE approaches like Wildfly Swarm?

Spring Initializr

Spring Initializr was the first pleasant surprise. Yeah, I know I must have spent the last couple of years in a hole, at least when it comes to Spring. So I missed Initializr until today. It's a good way to get started. Easy as pie. Just a few, simple questions, and you've got your running starter project. I chose to build a project with Java 11, JPA, the H2 database, and web programming.

Within a few seconds, I could download a straightforward project. Basically, it contained an empty configuration file and a single-line main class. Nonetheless, running mvn install started a server and showed that the database connection is up and running. Nice! That's what I expect from a convention-over-configuration approach.

Inspecting the auto-generated database

Adding a single line to the application.properties files activates another useful feature, the h2 database console:

spring.h2.console.enabled=true

Re-starting the server give us a new URL. http://127.0.0.1/h2-console is a nice little database client UI. You can run SQL queries and scripts, you can browse the database tables and so on. Maybe you already know PhpMyAdmin. The h2 console is pretty much the same, just automatically bundled with our Spring Boot application. Awesome! Especially for getting started quickly - which is why we're here today.

Too much magic?

The h2-console automatically fills in the database connection parameters. For some reason, it uses the wrong JDBC URL. Fill in the correct URL (jdbc:h2:mem:testdb), and everything works fine. On my machine, the wrong URL seemed to work - but it didn't show the new table. I suppose it created a new database with the wrong name on the fly.

That's the problem with magic. It gives you a head start. But you don't really know what you're doing. If things go wrong, you have to learn two things: how to configure the application without all the magic, and how to convince the magic to configure your application that way.

Let's enjoy some more magic.

@Entity public class Expense { @Id @GeneratedValue private Long id; private String hotel; private double amount; // plus getters and setters }

It took me a while to find the correct import statement. Don't choose the Spring annotations, but import the javax.persistence classes. Apart from that, magic works like a charm. Putting this file into the classpath is enough to have Spring create the table in the database.

REST and CRUD operations

When I started to implement the REST API calling services for performing CRUD[1] operations, I was in for a surprise. I expected something like this Spring.io tutorial. A long and tedious hour of work creating DAOs, DTO, Spring Services, and REST services. Luckily, there's a nice surprise: @RepositoryRestResource and PagingAndSortingRepository do all the hard work for you. I've dedicated an article to @RepositoryRestResource, so at this point suffice it to say "Nice!" again.

HATEOAS

I didn't request it, nor did I want it, but I got it nonetheless: HATEOAS. Every Spring REST service comes with a list of links telling you where you can go from here. In other words, they list related URLs:

{ "expense": [ { "hotel": "Grand Hotel", "amount": 456 }, { "hotel": "Petit Hotel", "amount": 221 } ] }

As you can see, the Json object Spring sends is a lot more verbose than expected.

In the age of Gzip, you can neglect the size penalty. It's there, but your application server optimizes most of it away. If it doesn't, teach your operations team how to activate Gzip. Or just do it yourself if you're working in a DevOps team.

Remains the problem that some clients don't cope with the verbose response. In the case of Angular, it's just a matter of using a couple of map() operators. I suppose it's similar to React or Vue.js.

If you really want to get rid of HATEOAS, I've got bad news for you. Of course, it's possible. But you can't do it using automagically. The Spring team is pretty dogmatic about that. For some reason, they confuse REST with HATEOAS. So the alternative is writing a lot of code Spring code generate for you if they were less enthusiastic about HATEOAS.

Comparing Spring Boot to Wildfly Swarm

Let me be honest: I can't seriously compare Spring Boot to Wildfly Swarm yet. In this paragraph, everything I'm telling you is an, well, educated prejudice. I hope I'm not completely wrong. If I am - or if you want to back me - send an e-mail to webmaster1 at beyondjava.de. Talking of which: I really hate that the comment function of this blog is a victim of GDPR. I hope to be able to restore it soon!

Java has this great feature call "hot code replacement." In almost every Spring application I've seen at the customers' this feature is broken. That's annoying because it forces you to restart the server after each and every edit.

However, I tried it in my test Spring Boot application, and at least some hot code replacement works. But even if it doesn't: the application restarts in four seconds. That's slightly faster than my Wildfly, which usually takes eight or nine seconds to start. Both startup times are hardly worth mentioning if you remember the old days. However, hot code replacement means instantaneous updates without a restart, so that's something to crave for.

As far as I can see, Spring Boot developers to just that: they develop their application with Spring Boot. That seems to be a difference to Wildfly Swarm. Compiling and bundling a Wildfly Swarm application takes some time. So most of the time, Wildfly Swarm developers prefer to use the traditional Wildfly application server. This approach works, but it's a bit odd to use one platform (the application server) to develop and another platform (Wildfly Swarm) to use for production. With Spring Boot, I can use the target platform for development. One source of errors less!

Wrapping it up

Spring and I, we have some history, much of it caused by corporate politics and power struggles. Consider me biased. Even so, I have to admit I'm impressed how easy it is to get your feet wet with Spring Boot in 2019. The Spring team went into great lengths to make everything run smooth, and the effort shows. Right now, I'm not sure whether I should recommend using Spring Boot of Java Enterprise Edition. What I can say is: no matter how you decide, you've chosen a great framework.

Just see to it to keep things simple. More often than not, I join a "historically grown" project. The common denominator of all these projects is that they're too complicated for the average programmer. Usually, the architect piles good ideas on good ideas until the result reaches the limit of their brain capacity. Problem is: at this point, the team has already ceased to understand how everything works, and has begun to employ cargo cult. Can you imagine how a newbie to the project feels?

Be that as it may, that's not Spring's fault. In 2019, Spring Boot allows you to write a lot of code with zero to none configuration. That's awesome!

Dig deeper

Getting started with Spring Boot, JPA, and H2

Spring Best Practices by Adam Ruka

Automagically create a REST service reading the database

Creating REST and CRUD services (the hard but flexible way)

Spring HATEOAS


  1. Create, read, update, delete - the basic operations to store objects in a database↩

Comments