The definitive guide to learning the Spring Framework

The definitive guide to learning the Spring framework

When I was trying to learn Spring, I didn’t understand many of the topics and principles that the Spring framework solved.

Topics like, Dependency Injection, Inversion of Control, Model-View-Controller, etc I very much struggled with.

Without understanding these core concepts first, I found myself learning Spring a very difficult goal to achieve.

I took longer than I expected because I would continuously jump around between learning the Spring framework and learning a supporting concept.

This isn’t the best approach to learning anything.

In this blog post I want to provide beginners with some guidance to not just how to learn the Spring framework but also what not to learn and get the best experience for the time you’re going to put in.

 

Introducing Spring

Introducing Spring

 

 

The Spring framework is very big.

It is so big because it covers so many facets of different core concepts that you may encounter in your programming career specifically Java Programming.

I don’t mean to overwhelm you, but it is what it is and learning Spring will take a very long time.

So what is Spring?

The Spring framework is an alternative to Java Enterprise Edition (EE). –I discuss the differences between Java EE and Spring in a later section.

Spring is built on top of Java EE and uses many core components from Java Standard Edition (SE), like reflection.

The reason why Spring was created is because it solves one important problem.

It simplifies Java development significantly.

Once you start “thinking with Spring” you won’t want to go back to your style of coding.

It’s absolutely magical how Spring speeds up the development process at a higher level than using lower-level components offered in Java EE..

I don’t want to go into detail on how the mechanics of Spring works, but it’s tempting because I’m so excited to write this blog post. But that is another article on its own.

I’ll discuss more on the architecture from a beginners level to simplify Spring and make it easier to understand.

Are you beginner?

Learning Spring Beginner

An important question you should ask yourself is Why do you want to learn Spring?

The above image illustrations how I felt learning Spring and how you are probably going to feel too.

If you’re learning Spring, what do you intend to do with this new-found knowledge?

In the world we live in… You can’t simply learn something to put under your batman utility belt.

Because it will go to waste.

But for the sake that you found this blog post and are reading it tells me that you may have a good reason to learn Spring.

I want to emphasize the importance of having a purpose in mind because the Spring framework is very large and will take many months to a year, especially if you don’t have any background in some of the core concepts that it covers.

So if you’re going to learn it, learn it. But don’t work 50% through and give up, knowing you wasted all this time.

Anyways.. We’ll move on.

The core concepts…

There are some important core concepts that you should know before learning Spring.

I speak from experience of trying to learn the Spring framework without truly understanding these concepts and I found it very difficult in doing so.

Believe me, you’ll have a much better time learning Spring if you understand these first and have an idea of how they work before you dive into Spring.

What are these core concepts that I keep going on about?!!

There are many of them and I’ll compile a list where you can learn them at the end of this blog post.

Beside that, additional resources provided in later sections do a nice job of explaining them.

Essentially there is a book, that I will discuss later that explains many of them.

This is the most popular Spring framework book out there and I encourage you to use this as a guide to learning Spring. (More on this later).

Avoid your curiosity

Avoid Curiosity

Your curiosity is good and bad, as I have written about this before.

But essentially your programmer curiosity is learning topics that evolve around your main focus.

What does that mean?

You’re probably going to learn stuff that are related in a way to the main thing that you’re learning.

This can be wanting to learn about Java Persistence API because you want to make use of Spring Data automatic JPA repositories.

I understand this need but if you learn at a very modular and systematic way, you sure can, just at a later stage.

If you are wanting to commit to learning Spring, the best advice in regards to learning is doing one step at a time.

I know it’s tempting to jump around between different sections from the Web tier to databases and so on.

But if you’re just starting out, it can be detrimental to your learning experience.

Take it one step (aspect) at a time.

Avoid your curiosity and focus on one thing. You will eventually get to that last thing you have been wondering about and the reward will be that much more greater!

On the up side,

If I didn’t have curiosity I wouldn’t be able to write this blog post and know as much as I do about Spring.

Well discuss these aspects in a later section.

 

Simplifying Spring

Understand that Spring is not just a Web framework or a Dependency Injection framework, but much more than that.

We’ll discuss what Spring basically offers in the later section.

Many of the core concepts like Dependency Injection and Web MVC are offered in many other frameworks too, so not only Spring.

The concept of Model-View-Controller hasn’t existed for Java EE until of recent (in the time of writing this article) but Spring has greatly simplified and supported MVC for many years.

Spring simplifies Java development because it illuminates a lot of boilerplate code.

Boilerplate code is code that you’ll often have repeated in many sections of your code with little to no alterations.

This could be connecting to the database, saving an item, and closing the connection to the database.

See? Not so different from saving Users in some other part of your application.

 

Understand the scope of Spring

So what really does Spring have to offer you as a framework?

Well Spring is a modular dependency framework (dependencies are libraries of classes or interfaces).

This means that if you want to add in web server specific things to make your Java application more like a web server, you’ll add in the Web and/or Web-MVC packages.

If you want to use a Database like MongoDB (Spring supports NoSQL databases) you can add Spring Data MongoDB supported packages

To clarify, Spring’s functionality is split up into modules and you aren’t locked in to everything, instead you’re able to choose which parts of the framework you need.

 

Four parts

Think of the Spring framework being divided into four distinct parts.

In Craig Walls book, Spring in Action, he has broken up Spring into these four parts and each part has many modules that fit into that specific parent part.

All these modules are what make up Spring.

I don’t want to re-iterate on his book and you’ll have a better time learning about them in-depth from his book.

Simply it goes like this:

1. Spring framework Core offers Dependency Injection and Aspect Orientated Programming (AOP) for building your application.

2. Spring framework Web Tier offers support for the Model-View-Controller pattern that isn’t supported in Java EE Servlets (only recently).

This also includes Spring Security and Spring WebFlow frameworks which fit into the web tier.

The Web tier is an easier alternative to working with but not directly Java EE Servlets API because you have the additional options to switch out the view template engine to some others.

See, Spring for Web is built on top of Java EE Servlets and provides additional features to the developer.

3. Spring framework backend like I mentioned, Spring offers easier ways to hook-in to relational, and NoSQL databases and the ability to have automatic data repositories (Data Access Objects).

These automatic DAO will write the implementation for you, so you won’t have to actually write any selections or insertions (in most parts).

4. Integrating Spring I know this part is tempting to look over. But I recommend that you understand and build a working application with the first three parts before you touch Integrating Spring.

I’ll discuss more on his book a bit later in the Getting Started section.

 

What is Java EE?

Java EE Logo

 

I used to wonder for a long time what and how Java EE the worked, I know this is pretty bad.

Java Enterprise Edition platform offers many new technologies and frameworks for building enterprise applications.

It has added support so you can create web servers that serve dynamic web pages (like .NET ASP pages).

Want to enhance your persistence (database) support, it has that too.

All of these are great… but it’s a lot more work than using low-level APIs.

Spring is built on top of Java EE and uses a lot of the core components from Java EE and simplifies it so that it’s easier to use.

You can still use all of the functionality offered by Oracle in the Java EE platform as you’ll use Spring, so you won’t be locked in at all.

Java EE is the platform that extends the normal Java Standard Edition, so you can create enterprise grade, scalable applications.

The only problem is that using it is a pain, especially having to set it up.

This is where Spring shines especially amongst other Java EE alternatives similar to Spring.

Spring greatly simplifies the development process.

The idea is that you should have to worry about setting up all the configuration like connecting to the Database, configuring your view (HTML) template resolvers.

View templates?

View template resolvers transform special templates that have Java code embedded in them to almost complete HTML page (depending on which templating engine you use).

The Java code or Express Language (subset of Java) will replace users name variable in a profile page.

Or they can loop over (iterate) some logic and write HTML fragment like <li></li> to produce a table of items.

Getting back…

So you shouldn’t have to worry about the details of configuration and so on, but rather the business logic.

If you don’t know what business logic is… It is the core logic of your application, that does all the calculations for your financing app or processing products for your eCommerance app.

This could be your payments system or your content serving system, what ever it may be, it’s the logic that gives your application a specific purpose.

If you want to learn more about Enterprise Applications and get a better understanding of “business logic”, check out Martin Fowler’s bliki (blog-wiki) on Enterprise Applications.

Things like setting up configuration is all boilerplate stuff, everyone is doing it and you are do the same thing over and over again. (I think I sound like Martin Fowler in my head :D)

Frameworks, specifically Spring abstract away a lot of this boilerplate stuff so you don’t have to do it.

Although not everything is free.

Spring will “configure” (I say “configure” because it using an opinionated choice at its core) a lot of the stuff for you (that is what makes it simple because it simplifies things, but that simplification may not work for you every time).

So in order to control Spring with what it does, you need to configure it too.

Configuration? Ahh yes lot of configuration.

Like I said, nothing is free and be-all-end-all.

Spring used to be bloated with much configuration and still is to some degree.

That’s where a delicious icing came that you can put on your Spring cake…

More on this later.

Should I learn Java EE first?

yes, man

The Spring framework is build on top of Java EE.

Okay, so it will make sense to learn Java EE first, right?

Well yes, you don’t need to know how to build a Java EE specific application but rather understand many of the underlying concepts.

In regards to Web tier of Java EE,

This is where Spring MVC is built on, you should learn about the Servlet API and how the Servlet container works.

Understand what Tomcat is and role it plays as a servlet container.

In the context of Databases, first understand how to interact with a relational database and use straight JDBC as your underlying mechanism because everything else like Java Persistence API is built on top of that.

In short, understand what Java EE is compared to the Spring Framework is as a platform.

Then understand how the aspect (e.g Web Tier) you are learning fits into Java EE and then learn the Spring part for that aspect.

The full Java EE 7 tutorial is here.

 

Spring Projects

There are a handful of projects that fall under the Spring Framework umbrella.

Remember as a beginner or even a well-adjusted Spring developer I would still encourage you to know how the Spring Framework works on its own.

Well enough that you are competent enough to build an application.

Spring Framework

Well you have Spring Core Framework, I have already discussed a bit on already.

This includes the Spring Core (Dependency Injection and AOP) framework and the Spring MVC framework.

Spring Boot

Remember all that Spring configuration and “nothing is free”.

Well Spring Boot is going to save you from all the configuration you’re going to be writing and it’ll write it for you by determining the context of your application.

Spring Boot is the icing on top of the Spring Framework cake.

Other Spring Projects…

There are some (many) other Spring projects that at some time want to look into.

But avoid the temptation and tame your curiosity to just learn the Spring Framework for now.

The Spring Framework and Spring Boot go hand-in-hand when it comes to Java development.

Spring is going to offer you awesome APIs and functionality while Spring Boot is going to speed up the development process but assuming a lot of your configuration for you.

It’s awesome what Spring Boot does.

 

Getting Started!

Okay Adrian, you have given me the “definitive” guide on what Spring is, how do I actually get started with learning it?

Wasn’t that the purpose of this blog post?

Well yes, but it took me months to figure that stuff out and now I can just explain what I have learned so you won’t have to as much of the same thing I did, struggle.

This is what this blog is about!

Everyone wants to have the cake and eat it.

Wait no that’s the wrong one!

Everyone wants to get the reward without putting in the work.

Let me tell you dear reader, this is no exception.

Like I said and I hope you have figured out that Spring is a very large framework.

I could only learn a good majority of it in only a handful of months, but that was full-time!

If you attend college, or have a full-time job, it will probably take you a lot longer too.

Expect that this is going to take a long time, six months to a year.

Okay, so let’s get started!

Have you seen this Quora question?

I just want to touch on this. A lot of the material I cover is referenced to that question.

Open it on a new tab and read it after this because it will fill in any additional gaps that I would have missed.

How do you learn Spring for the beginners?

I’m assuming that you don’t know all the core concepts like MVC, DI and IoC, etc.

(I’ll provide some links at the end that explain a lot of them.)

It’s going to be more difficult especially if this is your first framework.

You can learn the core concepts and will learn them along side learning Spring, but I highly discourage it.

Often when I was learning Spring, I would have to read how to implement Autowiring (Dependency Injection) but I didn’t understand the underlying concept clearly.

After learning the concept I would have the go back to the reference and learn Autowiring again.

I lost a lot of time doing it this way, so don’t do it too because it will be very frustrating.

 

The book to learn

Let’s just start with the book…

Spring in Action

 

 

 

 

 

 

 

 

 

 

 

 

Craig Walls, Spring in Action.

If your cheap you can nik a PDF off some dodgy website somewhere; but instead buy the book because it is so well written and one of his best works.

This is the best book to read if you’re wanting to learn Spring, plain and simple

He explains everything from a high level in a real-world scenario and then implements based on that explanation using Spring.

It’s quite fun to read too!

The four parts…

Remember the four parts I discussed earlier so you can understand the scope of Spring better?

We’ll this book explains Spring in those four sections (parts).

How to learn?

Don’t take this book (as magical as it seems) to heart.

Rather use it as your curriculum guide to learning Spring, well because this book isn’t be-all-end-all or any silver-bullet (like everything else).

It’s not going to show you well enough how to implement something, but rather how it works from a base perspective view.

If the author discusses the topic Dependency Injection, learn as much as you can from other resources as well.

Look up tutorials on Spring Dependency Injection and try to figure out how to implement it.

The rule is not to move on until you have a thorough understanding on the primary topic and you can build something with it.

This is by far the best way to learn because you’ll do yourself a disservice if you read along and forget about a lot of the stuff you have learned as you move on.

I would recommend that you build the “Spittr” application along with him (even though he doesn’t provide the sources clearly).

I think he intentionally made it difficult to implement his application because he is encouraging you to figure out all the details on your own, although he doesn’t specify this directly.

 

Spring Getting started guides

I wouldn’t recommend Spring guides as a beginner.

Well because a lot of the guides are formed on the idea that you already know Spring, pretty well.

These guides act as recipes to solving very specific problems.

They don’t offer much depth from a learners point of view on how Spring works.

But there is some additional repetitive boilerplate explanations that you might find useful, such as how to set up a project and some of the Spring annotations explained.

But don’t count on it.

Only go there if you’re facing a certain problem, like I was the other day trying to figure out how to upload file for my micro blog application.

 

Can I get some useful blogs?

Plain and simple, if you don’t read blogs and wanting to become a competent software developer you’re doing yourself a disservice.

I can go on and on to which blogs are useful for software development, but that’s another blog post (good idea).

Spring framework blogs, they are very useful for examples and specific problems too.

But as a beginner, learning what’s new should NOT be a concern to you as your focus is on learning Spring itself.

So for now stay away from the latest news blogs and stick to only a handful.

Mkyong.com offers great guides to implementing example applications. He explains many aspects in detail that’s easier on the reader, especially as a beginner.

 

Video tutorials

I was first hesitant with learning from video tutorials because I learned far much more from reading the documentation…

But after a long day it’s easier to comprehend by someone explaining something to you, especially if they  understand it better than you.

There are a bunch channels on YouTube that explain this stuff.

Telusko Learnings offers great explanations from the ground up on Spring and Java, I like him the best because he explains things so well and is geared to self growth and self-help.

Java Brains is excellent because of how he teaches. Although I have just recently stumbled on his videos, he offers great content for Spring and Java, plus more!

 

Additional Resources

Okay I did promise some resources that you should check out first before diving into the Spring framework and some other useful resources.

Introducing Spring

Introduction to the Spring framework at Spring Docs

Dependency Injection/ Inversion of Control

Spring Dependency Injection by DZone

Spring DI Example by SpringFrameworkGuru

Model-View-Controller

Mastering Spring MVC by JavaWorld

Spring MVC Explained (video) by Telusko Learnings

Java EE Servlet API explained (video) by Telusko Learnings

Book

Craig Walls, Spring in Action book on Amazon

Martin Fowler’s Enterprise Application bliki article

Supporting Questions

How to Learn Spring Framework? Question at Quora.com

What are the differences between Java EE and Spring? Question at Quora.com

Conclusion

I hope I have cleared up any confusion you may have and given a better insight to what Spring is and what it’s not.

Learning Spring takes a long time.

If you’re just starting out with learning a framework for the first time it’s going to be tough to read and comprehend a lot of the new topics.

This goes for everything really.

Good luck learning Spring!

Adrian van den Houten

I'm Adrian van Houten, founder of ScholarCoder and a passionate software developer for full-stack web development. Read more about me here.