Spring Boot – just run your application

Marcin Rozmus

Spring Boot is an framework, based on top of Spring Framework, to build Java applications as fast as snapping fingers. It provides things such as connectivity to databases, metrics, auto-configuration. You don’t have to configure the low level code to get up and running, just use Spring Boot and get off the ground very quickly.
It’s also very good in case of developing your application – stop wasting your time for boilerplate configuration – just add some dependencies and run the application. It is as simple as it sounds.

How to start?

You have to choose between starting new project using Spring Boot or adding it to existing one.

New project

To start from scratches simply use start.spring.io to generate new project:  On this page you need to specify what version of Spring Boot to use and enter basic metadata. You can also choose version of Java and add more dependencies to your project. If you decide to do it Spring Initializr add compatible version of chosen dependency, for example, if Apache Camel will be chosen, in you dependencies will show up:

<dependencies>
    <dependency>
        <groupid>org.apache.camel.springboot</groupid>
        <artifactid>camel-spring-boot-starter</artifactid>
        <version>3.0.0</version>
    </dependency>
</dependencies>

if project is based on Maven, or:

dependencies {
  implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.0.0'
}

if it using Gradle.

Existing project

Secondly and probably more likely option is if you have project previously created and you want to add Spring Boot to it. But no worries, that project is using Spring Framework, it’s as simple as in previous point. From this point to the end we will also assume that we are using Maven – to keep examples short. So, to do that just add:

  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.2.2.RELEASE</version>
  </parent>

in your main pom.xml file. For now Spring Boot will take care of your dependency management. To avoid unexpected behaviour you should remove any explicitly previously defined dependency version which is also managed by Spring Boot.

Next step is to add entry point to your application. To do that create new class with main method (or if your application is a non-web application which creates an ApplicationContext – replace it with):

package pl.jlabs.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) { 
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }

}

By default Spring Boot scans all classes in the same package or below. If you have existing @Configuration or component classes to be picked up, but in different location there are two options:

  • move the existing classes to a package that is the same or below your SpringBootDemoApplication class package
  • import the classes explicitly

In second case do it by specifying packages path in @ComponentScan or even by including individual classes with @Import:

@SpringBootApplication
@ComponentScan(basePackages="pl.jlabs.config")
@Import(UserRepository.class)
public class SpringBootDemoApplication {
    //...
}

You have to also migrate your application resources and properties. For resources – move yours files to new directory called /static. Another allowed directory names are /resources/public or /META-INF/resources.

To define completely different directory location add new property:

spring.resources.static-locations=classpath:/images/,classpath:/angular/

For properties – move it to application.properties or application.yml file in one of the following locations:

  • /config subdirectory of the current directory
  • /config directory on the classpath
  • the current directory
  • the classpath root

For example, in projects with maven/gradle we can move application.yml to src/main/resources – and it will be present on the classpath.

Web application

In most cases application that you want to migrate to Spring Boot is a web application. To simplify that Spring Boot offers us starters – a set of dependency descriptors ready to be included in your application. They contain all required dependencies and custom properties that allow you to add new functionality easily.

We can remove our all web-related dependencies from the Spring framework and replace them with spring-boot-starter-web:

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-web</artifactid>
</dependency>

If you don’t need to create web application you can replace this import with:

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter</artifactid>
</dependency>

We can also simply run (like in title!) our web application on embedded Tomcat server, by adding:

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-tomcat</artifactid>
</dependency>

This server will be run locally on port 8080.

Other web servers supported by Spring Boot’s auto-configuration are Jetty and Undertow.

Now, to run your application just click „run” button in your IDE:

or add:

<build>
    <plugins>
        <plugin>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-maven-plugin</artifactid>
        </plugin>
    </plugins>
</build>

in your pom.xml and use:

mvn spring-boot:run

In both cases you should see Spring Boot logs:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2020-01-20 15:10:24.421  INFO 10804 --- [           main] p.j.s.SpringBootDemoApplication          : Starting SpringBootDemoApplication on DESKTOP-8A5TSLS with PID 10804 ...
2020-01-20 15:10:24.425  INFO 10804 --- [           main] p.j.s.SpringBootDemoApplication          : No active profile set, falling back to default profiles: default
2020-01-20 15:10:25.489  INFO 10804 --- [           main] p.j.s.SpringBootDemoApplication          : Started SpringBootDemoApplication in 2.264 seconds (JVM running for 6.158)

Conclusion

Amount of work you have to put in to add Spring Boot to your project is too small to not take advantage of the benefits. As a result of our steps you are getting application that can be easily run and debug on every developer’s machine without any time-consuming preparation.

Spring Initializr
Spring Boot Reference Documentation

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami