9.2. Initialize Spring

Spring Boot is a framework used to build Java web applications that implement the MVC pattern. For more about Spring, check out Spring IO.

9.2.1. Initialize a Spring Boot Project - Video

9.2.2. Initialize a Spring Boot Project - Text

  1. Go to start.spring.io.

  2. For Project, select Gradle Project.

  3. For Language, select Java.

  4. For Spring Boot, select 2.2.x (that is, the most recent 2.2 non-SNAPSHOT release).

  5. For Project Metadata/Group, enter org.launchcode.

    Note

    You can use whatever package name you want here. The convention is topleveldomain.domainname. In other words, the reverse of what you would see in a URL.

  6. For Project Metadata/Artifact, enter hello-spring.

    Note

    This is the title of your project.

  7. For Project Metadata/Options/Java, select 13.

  8. For Dependencies, search for and add the following: Spring Web, and Spring Boot DevTools.

  9. Click Generate to create a .zip file of the project starter code.

Spring initializer with options

Spring Initializer with desired options selected.

9.2.3. Spring in IntelliJ

  1. Move the downloaded unzipped folder from your downloads folder into another location such as LC101 or your home directory.

  2. Start IntelliJ.

  3. Select Import Project and browse to where you put the downloaded project.

  4. Select Import project from external model and pick Gradle. Select all other defaults as you create the project.

    Note

    If you encounter an alert that the selected folder is not empty, choose the option to overwrite it.

  5. If you see a Gradle build popup, go ahead and click Import Gradle Project.

    Popup to import Gradle

    A popup to suggest importing Gradle.

    Note

    This popup is fairly small and usually located in the lower right corner of the window.

  6. If a window then opens, check the Use auto-import box and leave everything else as it is.

Note

You may see a pop-up in the lower left-hand corner in your Event Log that reads: Unindexed remote maven repositories found.

  1. Select Open repositories list in the Event Log message. This will open the Preferences (or Settings for Windows users) window to Build, Execution, Deployment > Build Tools > Maven > Repositories .
  2. Select the Maven repository (https://repo1.maven.org/maven2) and click Update on the side.

Warning

This update usually takes about 20 minutes but can last up to an hour.

9.2.4. The Spring Project Structure

The Spring Initializr gives us a number of helpful files and directories to get our Spring project up and running.

Within the src directory, you’ll find a familiar setup with both a main and test directory. Inside of main will be a java folder to house your packages and classes. Inside the java folder is a HelloSpringApplication. Opening this for the first time, you’ll likely see some import errors that can be resolved by clicking on the context menu to add the corresponding library to the classpath. Expanding the main method shows you the line that allows the application to run. We’ll run the app in the section below but won’t be working with much else with the main method in this course.

You’ll also find that there is a resources folder in main. resources is meant to hold your non-java code.

Outside of src, a .gitignore contains the basic items of our project that Spring expects to not be version controlled. These are files created by the IDE, the compiled code in the .build directory, and the gradle .jar and directory.

9.2.4.1. Gradle

Gradle is a Java tool that handles much of the work related to building and deploying software applications in Java. Your Spring project contains a file called build.gradle. As with most new tools, you do not need to know everything that this file does. For now, the takeaway is that Gradle manages the external dependencies in our project.

Remember specifying the dependencies of the Spring project? Scroll down to the bottom of your build.gradle file and you will see these items specified in a structure called dependencies.

22
23
24
25
26
27
28
29
dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   developmentOnly 'org.springframework.boot:spring-boot-devtools'
   testImplementation('org.springframework.boot:spring-boot-starter-test') {
      exclude group: 'org.junit.vintage’, module: ‘junit-vintage-engine'
   }
}

Gradle fetches these external packages from another location on the Internet so that we can use them in our project locally. That location is called the Maven central repository. The Maven central repository is a decentralized place for developers to post their code for others to use.

9.2.5. Before Running a Spring Project

Before running your project, we need to check the configuration of a few items.

#. In build,gradle, locate an item called sourceCompatibility. If it’s value is not equal to 13, change it and save the file.

#. In the gradle directory, open the subdirectory, wrapper, and then the file gradle-wrapper.properties inside of it. Ensure that the distributionUrl includes a gradle version of at least 6.0.

  1. Open the test class, HelloSpringApplicationTests located in src/test/java/org.launchcode.hellospring. Run the single test. If you experience a JDK error, click on the Gradle Settings icon in the right-hand gradle pane and ensure that the Gradle JVM is using Java 13.

9.2.6. Running a Spring Project

To run the application, click on the Gradle icon on the side of your IntelliJ window. If you don’t see the Gradle side bar, click the panel icon in the bottom left corner of your window and select Gradle.

Panel icon options expanded

Hovering over this icon opens options for panels to open.

Once the Gradle panel is opened, go into Tasks. Here you’ll find the various tasks that Gradle can do for your project. Inside Tasks, open application and double-click bootRun. This will run our Spring Boot application.

Gradle panel view with bootRun selected

Gradle panel view with bootRun selected.

Once the bootRun task is executed, you should see a new panel with output similar to this:

bootRun output

Output of the bootRun task.

Note

Also note that you may not see the same output in the Gradle panel as is shown above. You may see something that looks more like this:

Alternative bootrun view

Click the circled icon to switch the view.

If you do, click the circled icon to toggle the view so that it matches the one above.

Keep an eye out for a message that resembles:

Started HelloSpringApplication in 1.739 seconds

While this message will change depending on the application you are running, this message indicates that the Spring Boot started up successfully. You’ll see an error message or a stack trace if there is an issue with your project preventing it from starting.

You’ll also notice a message above:

Tomcat started on port(s): 8080 ...

Tomcat is the embedded web server within your Spring application. So this tells us that Tomcat is listening for requests on local port 8080. You can then visit the corresponding web page at localhost:8080. Right now, you’ll see an error page, but we’ll fix that soon. Now go ahead and stop the application by hitting the red square on the left side of the Run pane.

Tip

There is also a stop button up at the top right of the IntelliJ window.

9.2.7. Check Your Understanding

Question

What is the name of the task to start a Spring Boot application?

  1. Gradle
  2. bootRun
  3. run
  4. Maven

Question

True/False: All custom code in a Spring Boot application is located in the main method.

  1. True
  2. False