Getting to know Spring Boot Application

Abhishek Jindal
4 min readJun 24, 2021

--

Introduction

In this article we are going to touch upon the details of a brand new Initialized Spring Boot Application that can be generated from Spring Web Initializer, Spring Tool Suite or an IDE. There are mainly four files to look at initially -

  1. Application.java — The application’s bootstrap class and primary Spring configuration class.
  2. application properties — A place to configure application and Spring Boot properties.
  3. ApplicationTests.java — A basic integration test class.
  4. build.gradle or pom.xml — build specification.

Bootstrapping Spring

@SpringBootApplication
public class ReadingListApplication {

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

}

Application.java class serves two purposes -

  1. Configuration — though Spring Boot Auto-Configuration eliminates the need for a lot of configuration, a small amount of Spring configuration is still needed.

@SpringBootApplication — Enable component-scanning and auto-configuration. It combines three other useful annotations -

  • @Configuration: Designates a class as a configuration class using Java based configuration.
  • @ComponentScan: Enables component-scanning so that the web controller classes and other components we write will be automatically discovered and registered as beans in Spring application context.
  • @EnableAutoConfiguration: As the name suggests, it enables Spring Boot’s magic auto-configuration.

2. Bootstrapping

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

There are several ways to run a Spring Boot application. Including a traditional WAR file deployment.

Also, main() method enables the application to run as JAR file from command line. It passes reference to the Application class to SpringApplication.run(), along with command line arguments.

Ways to run a gradle based SpringBoot application -

  1. Using bootRun task from Spring Boot’s Gradle plugin
$ gradle bootRun

2. Build the project with Gradle and then run uber-jar (includes packing all of the application’s dependencies within the JAR and adding a manifest to the JAR with entries that make it possible to run the application) with java at command line.

$gradle build$java -jar application-0.0.1-SNAPSHOT.jar

Ways to run a maven based Spring Boot application -

  1. Using run goal from Spring Boot’s Maven plugin
$ mvn spring-boot:run

2. Build the Project with Maven and run uber-jar with java at command line.

$ mvn package$ java -jar application-0.0.1-SNAPSHOT.jar

Testing Spring Boot Applications

@SpringBootTest(classes = Application.class)
@WebAppConfiguration
@TestPropertySource(locations = "classpath:test.properties")
class ApplicationTests {
@Test
void contextLoads() {
}
}
  1. @SpringBootTest — helps create application context containing all the objects we need for the tests.
  2. @WebAppConfiguration — is a class level annotation used to create a web version of the application context in Spring Framework. It denotes that the ApplicationContext which is bootstrapped for the test is a WebApplicationContext
  3. @TestPropertySource — helps configure locations of properties files specific to our tests. It overrides the existing application.properties file.
  4. contextLoads() — It is star method annotated with @Test that verifies every bit of functionality provided by the application when it is generated.

Configuring Application Properties

application.properties file is a completely optional file that comes in handy for fine grained configuration of the stuff that Spring Boot automatically configures.

Examples include setting up port number for the Tomcat Server to run the application on using “server.port=9000”, etc.

Using Starter Dependencies

Spring Boot Starter Dependencies helps pull a list of dependencies usually required together for supporting a particular functionality in a Spring application without worrying about their versions compatibilities thus helping the developer to focus on business logic rather than searching for dependencies and testing their compatibilities. Moreover, there is no need to worry about versions of the libraries needed since the libraries pulled by starters are tested together.

Spring Boot provides dozens of starter dependencies whose list can be seen here — link

Overriding starter transitive dependencies:

We can certainly add new dependencies, remove existing (unused) transitive dependencies or update (versions of) transitive dependencies in our builds.

  1. Removing transitive dependency — For example, Spring Boot’s Starter JDBC transitively depends upon HikariCP (a lightweight library for JDBC connection pool). If we want to remove it from the build, we can use the following ways -
  • Maven -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
  • Gradle
compile(“org.springframework.boot:spring-boot-starter-jdbc”) {
exclude group: ‘com.zaxxer’, module: ‘HikariCP’
}

2. Updating version of transitive dependency -

Case 1 (Updating to a newer version): Suppose Spring Boot Starter Test uses JUnit v5.6.3 but we want to use the newer version i.e. v5.7.2 for our tests. We will simply add the dependency with newer version in build file (build.gradle or pom.xml). Maven and Gradle always favors the closest dependency, meaning that because we have expressed this dependency in our project’s build, it will be favored over the one that’s transitively referred to by another dependency.

  • Maven
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>

</dependency>
  • Gradle
compile(“org.junit.jupiter:junit-jupiter-api:5.7.2”)

Case 2 (Updating to older version): In this case, we will have to exclude the transitive dependency and explicitly provide the dependency version in build configuration.

  • Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>

</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>

</dependency>
  • Gradle
compile(“org.springframework.boot:spring-boot-starter-jdbc”) {
exclude group: ‘org.junit.jupiter’, module: ‘junit-jupiter-api
}
compile(“org.junit.jupiter:junit-jupiter-api:5.7.2”)

--

--

Abhishek Jindal

SWE at Edifecs, Technology Enthusiast, Art admirer and creator.