Getting Started With Spring Boot … Part 1
By: Lakshmanan Subbiah
Tags: Eclipse, Spring, Getting Started, Spring Boot, Web App Development

Spring is an application framework that provides complete infrastructure for the development of Java applications. It’s packed with nice features such as Spring MVC, Spring JDBC, Spring Security, Spring ORM etc.

These modules reduce significant time in the development of applications. For example, In early days of Java web development, We need to write a lot of boiler plate code to insert data into database. But with Spring JDBC it is reduced to very few lines in terms of configurations.

The most important feature of Spring is Dependency Injection. At the core of all Spring modules is Dependency Injection or Inversion of Control(IOC). With DI and IOC we can develop loosely coupled applications. And the loosely coupled applications can be easily unit tested.

One significant overhead with spring is that the amount of configuration that has to be done while developing applications

Spring Boot solves this problem with the combination of Auto Configuration and Starter Projects.

Spring Boot:

Spring boot is an open-source Java Framework used to build production grade Spring Applications. By definition, it is an application framework and inversion of control container for Java Platform. We’ll discuss about inversion of control in our upcoming posts.

Essentially Spring Boot is a project built on top of Spring framework i.e an extension of Spring framework which eliminated the configurations required to setup an Spring application.

source : javatpoint

Enough with the introductions and definitions, Let’s kick start the development of Spring Boot application with a sample app

We can easily create and run a spring boot app in 15 minutes. All we have to do is visit Spring Initializr and create a spring boot project with the dependencies of our choice.

source : https://spring.io

We will use two dependencies, Spring Web and Spring dev tools ( tools highly useful for spring development)

Click Generate, download the zip and unpack it into a folder in your computer.

We can run the spring boot app directly without any IDE by navigating to the folder and using the command

./mvnw spring-boot:run

in Mac or

mvnw spring-boot:run

in windows.

In this post, we will see importing your project into eclipse and running the spring boot app via eclipse

Open Eclipse App and go to Help → MarketPlace Search for Spring Tools . Install the Spring tools plugin and restart the eclipse.

Now go to File → Import → Maven ( Existing Maven Projects) → Browse the folder location → finish

Now you can see the folders & files present in the project in Project Explorer

we can see two classes generated automatically SpringGettingStartedApplicaiton.java & SpringGettingStartedApplicationTests.java — Spring boot by default generates test folder and encourage us to write unit test for our code.

Now click on the SpringGettingStartedApplication.java file we can see a main function. We are going to write a sample get API and run it as an spring boot app via eclipse

Initial Code auto generated in spring app

Detour @SpringBootApplication annotation:

SpringBootApplication — its an annotation unique and newly created in spring boot that comprises of three annotation provided in spring @EnableAutoConfiguration, @ComponentScan, and @Configuration.

Enable Auto Configuration annotation specifies the spring boot to look through the dependencies and configuring them automatically reducing lot of work for developers ( for example configuring data source if hibernate jar is present). It is implemented in spring-boot-autoconfigure.jar ( ref : https://www.springboottutorial.com/spring-boot-auto-configuration )

Component Scan annotation specifies the application where to look for beans(components) i.e Once you define a Component Scan for a package, Spring would search the package and all its sub packages for components/beans.

(note : If all the packages you develop in your application is sub packages of the main package @SpringBootApplication is enough. Or else you have to explicitly mention component scan annotation)

Configuration annotation on top of any class to declare that this class provides one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.