Thursday, March 22, 2018

Maven and Spring Boot Basics

Hello Everyone,

By this post I will talk about what maven is and it's uses along with depth knowledge about Spring Boot.

Let's first talk what maven is.........

Maven automates downloads dependencies, putting additional jars on a class path, compiling source code into binary code, running tests, packaging compiled code , minimizing the risk of humans making errors while building the software manually and separating the work of compiling and packaging our code from that of code construction.

Inorder to check whether your computer has installed with maven check below command,



What is a POM?

A Project Object Model or POM is the fundamental unit of work in Maven.It is XML format which contain all the dependencies and configuration details project need to process and get build.



Few commands that maven uses
  • mvn clean       - previously generated artifacts will be removed
  • mvn compile   - compile source code
  • mvn package   - bundle is created
  • mvn install      - package is copied to local repo
  • mvn deploy     - final package is copied to remote repo
  • mvn test          - testcases are executed


Before talking about Spring Boot let's talk about spring framework......

Spring is a lightweight framework which act as a framework of frameworks. It provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc. The framework is used to find solution to the various technical problems.

The Spring framework comprises several modules 


  • IOC
  • AOP
  • DAO
  • Context
  • ORM
  • WEB MVC 

What is a Dependency Injection (DI)?

By the name it gives you the maening. Dependencied are injected to classes rather than allowing them to have links to other classes.
This reduces coupling between each classes. DI is a specific type of Inversion Of control(IOC)


 Figure 1

 Figure 2


In figure 1 there is a tight coupling between address and student.If the address class get changed, the student class also need to be changed.But in figure 2 Address Object is passed to the constructor to set the value which reduces the coupling between the address and student.

DI can be achieve through using 3 methods.

● Setter Based
@Autowired
public void setName(StudentService sv){
this.name=sv;
}


● Constructor Based
@Autowired
public Student(StudentService sv){
this.student=sv;
}


● Fields
@Autowired
private CacheMonitor cache;


Now let's talk about what Spring Boot do...

Why to use Spring Boot?

Though Spring is a good framework, It is hard to configure and manage the code while it takes some time to bootstrap the project. But by using Spring Boot we can minimize those drawbacks.Integration of the 3rd party libraries are highly supported by Spring Boot. Presence of integrated server to run the application also provides Spring Boot more special.

Starters can be downloaded from the Spring Boot website easily.Below are some starters used by spring boot..


  • spring-boot-starter-security 
  • spring-boot-starter-web-services
  • spring-boot-starter-integration
  • spring-boot-starter-validation
  • spring-boot-starter-actuator

First Spring Boot Code

Bootstrapping the application.



By using run method, Spring Boot server gets started.By annotation @SpringBootApplication we tells to the application that we want to run the application using spring boot.

Let's see how Rest controller can be used with spring boot..



Using the browser we can navigate to the localhost and relevant port number we can get the output as hello world.This is the base of building large web services.

Let's meet again with another exiting post.Happy coding :)

©Copyright Viraj Wickramasinghe.

No comments:

Post a Comment