Monday 29 May 2017

Getting started with SpringBoot!

Before starting SpringBoot, Let me tell you, How did I find Spring Boot.

I have started my career with Play Framework. Play is popular for its simplicity. Play almost required no configuration. It does not need to write any XML, yes, I repeat No XML.

Play by default provides below things:

1. DataSource - Hikaricp
2. Logger - SLF4J with LogBack
3. Dependency Injection -  Google Juice
4. Build tool - SBT (Simple Build Tool)

The play comes with Scala and Java. It supports Non-blocking IO.

After exploring play enough, I have started working on Spring rest (Jersy) with Angular. For play developer, it's difficult to write boiler code for Spring. Spring required a lot of boiler code.
I have started exploring seed projects for Spring, I found few useful (To avoid writing boiler code).
While watching one of the Devoxx videos, I came to know about SpringBoot. I started Exploring Spring Boot example. I was very Happy With SpringBoot. It required almost Zero Boiler code.





1. No Need of Tomcat.
2. No Need to write XMLs

Within a few minutes, I was able to create a simple SpringBoot application with HelloWorld rest API. It's almost like working with Play framework again.

SpringBoot comes with Maven, Gradle. I choose Gradle, as, I was already familiar with Maven.

To understand SpringBoot, I have developed the REST API for the retail manager.

The problem statement is as Below:

A basic Spring Boot Application for the retail manager. Shops are stored in the Java in the memory map. Nearby shops can be retrieved by providing latitude and longitude of a customer. The distance between customer and shop is calculated by Haversine formula! The latitude and longitude of a shop are calculated using Google map API.


I have pushed all the code to Github: https://github.com/prashant7090/retail-manager

Let's understand springBoot. SoringBoot provides annotations to specify controller, model, service layer.

@SpringBootApplication
@ComponentScan("com.retailmanager")
public class BootRetailManager {
    public static void main(String[] args) {
        SpringApplication.run(BootRetailManager.class, args);
    }


This is the starting point of SpringBoot.  @SpringBootApplication annotation loads components required for springBoot. @ComponentScan("com.retailmanager") - annotation by default scans spring components in child package, or, we can specify package.

@Controller
@RequestMapping("/shop")

@Controller annotation to specify controller.   @RequestMapping("/shop")   used to create route for the request.

@Autowired
GoogleApiUrlBuilder googleApiUrlBuilder;

@Autowired annotation to do dependency injection.

@Component
public class ShopDaoImpl implements ShopDao

 @Component annotation to indicate this is the class is an auto scan component. @ComponentScan  find out the bean (annotated with @Component) and register it in Spring container.

We have different annotations like @Service, @RestController etc, I have not used those. Rest of code is preety simple. (DAO layer).

Happy Learning!





No comments:

Post a Comment