How do I get started with Spring?
Software developers often ask me what the easiest way to get started with Spring is. There are, of course, multiple options for getting started. This post lays out a simple yet effective way to get started with Spring.
One of the best things to happen to Spring is a project called ‘Spring Boot’. In my opinion, this is the quickest and easiest way to get started with a Spring project.
Spring Boot is a project from Pivotal Software that is designed to get you up and running fast. As the project website states, “Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can ‘just run’. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.”
What’s even more exciting about Spring Boot is the Spring Initializr site at start.spring.io This site will generate a Spring Boot project skeleton (choose either Gradle or Maven), let you set project metadata as well as which dependencies or Spring Boot Starters you want to include in your project. You then click the ‘Generate Project’ button to download a zip archive of your skeleton project. How cool is that??
There’s also a ‘full version’ from the same start.spring.io page if you want to choose options like Java Version, Language, and specific Spring Boot Starter dependencies.
OK, what’s the first step?
To get started, head over to start.spring.io From there, choose ‘Gradle Project’ and ‘Spring Boot 1.4.0’:
Or, if you’d prefer, you can get this project directly from my github repository – simply visit my Spring Beginner Tutorial repo where you can clone or download this Spring Boot project.
Specify Project Metadata:
Under dependencies, just leave this blank for now.
Click Generate Project and download a zip of your project skeleton. Extract this into your project workspace and head over to Eclipse.
Open your project in Eclipse
From the Eclipse menu, choose File > Import > Gradle Project > Next
Specify the root directory of your project. Eclipse will then import your project.
Running a Spring Boot application
Now let’s go ahead and run our newly created Spring Boot application. Right-click on the project and choose ‘Debug As’ > ‘Java Application’. If, at this point, you get a dialog asking ‘Select Java Application’, go ahead and choose ‘ProjectNameApplication’, where ProjectName is the name you gave your project. Usually it’s the top option in the list.
In the console output, you’ll see several lines printed out. Once you see a line like the one below printed out, you know that your Spring Boot project is up and running:
INFO 10076 --- [ main] c.d.s.SpringBootIntroApplication : Started SpringBootIntroApplication in 2.833 seconds (JVM running for 3.426)
What does this project actually do?
If you followed my instructions exactly, you ended up with the basics of a Spring project, with a few essentials. Take a look at the debug output, in particular the following lines:
INFO 10076 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
INFO 10076 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
INFO 10076 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
INFO 10076 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
INFO 10076 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
As you can see, the following gets launched as a part of running our Spring Boot application:
- Spring WebApplicationContext
I’ll get into more details around the different context options in later blog posts. For now, just know that the Spring WebApplicationContext, along with the Tomcat Servlet Engine, are what allows your application to accept web requests. With your project still running, open up a web browser to http://localhost:8080 and see what response you get. You should see something similar to the following:
Don’t be alarmed with the 404 response, it’s actually expected. The 404 is actually coming from your Spring Boot application. A 404 is being returned because we still have more work to do. We need to map URLs to methods in our Spring Boot application to handle the request that come to the application. In future blog posts, I’ll get into those details.
Good job! You’ve now created your first Spring (via Spring Boot) application! It’s a simple application, but has all the pieces necessary for a Spring project. Spring Boot helped us along the way; it’s a great way to get a Spring project up and running.