Web based application Structure Springboot

 

Introduction

  • Spring Frame work configuration is light weight compared to EJB (Enterprise Java Beans).
  • But there are a lot of complains about XML config files based configuration due to the number of files needed. 
  • Later annotation based config is used but didn't solve the problem. Spring boot was introduced and it solved a number of problems. 
  • Spring Boot sits on top of spring framework & makes it easy to set up, configure & run web applications. It is extension of spring framework that helps developers build web based applications quickly & easily by removing lot of boilerplate code & configuration.


Structure of a web based application

  • All of the code for starting the application, configuration is repetitive &  boilerplate. 
  • Spring Boot solves they by emphasizing Convention over configuration approach & is smart enough to choose dependencies & auto-configures what you need, so we can launch our application with single click.  Refer the above diagram.

Features of Spring boot

  • Automatic configurations
  • Simplified Deployment
  • Starter Dependencies

Automatic configurations

  • Spring Boot follows convention over configurations configured approach.
  • It automatically configures application based on libraries added to project class-path. By doing so, it saves a lot of time.

Eg. If you include H2 DB in pom.xml file, Spring Boot auto configures application to the Database you need.

Deployment

  • Spring boot simplifies deployment with as it comes built-in servlet container like tomcat & Jetty embeds directly. There is no need to deploy WAR files separately.
  • No need to download, install webservers & Package your application & saves a lot of time.

Starter Dependency

  • A starter dependency is like a one-stop shop which includes all of the libraries you need.
Eg. spring-boot-starter-web, spring-boot-starter-test, spring-boot-starter-data-jpa

  • spring-boot-starter-test contains Junit, Mockito, Hamcrest, Spring-core, spring-test 
  • spring-boot-starter-data-jpa contains Spring data JPA with Hibernate, JDBC, Entity manager, Transaction API,  Spring DATA JPA. 
  • spring-boot-starter-web contains web application development & Spring MVC, tomcat, REST & Jackson for JSON to Java object mapping

Spring Boot Actuator

Spring boot Actuator helps to see what is going on inside a running Spring Boot application. It helps to

  • Monitor running Application (See Runtime information like heap memory, threads usage etc.) 
  • Manage and monitor via HTTP endpoints of JMX 
  • Set Health status, metrics about application, loggers, audit events, HTTP race etc.

Auto-configuration working

  • Find JARS on the classpath & auto-configures "beans automatically. For example, it autoconfigures for data source for Hibernate.
  • If you add your own bean like data source bean, default embedded data base support backs away. 
  • Auto config is always applied after user registered beans are deployed.
  • To find what auto-config your application is using 
    • start application with --debug mode 
    • Add below log property to application.properties
      • logging level.org.springframework: DEBUG
    • In the logs, you can find report for positive and negative matches.


Spring Boot profiles

Profiles are fundamental to application. For most use cases, we will  have an application and it needs to be deployed to different environments like DEV, TEST & PROD. 

In src > main > resources,  there is already an application.properties file.

The properties which will be common across all environments will be written here. 
Now, In the same location let’s create a file with name application-local.properties. 

Let’s use application-{env}.properties as convention for different environments. 

When the application is deployed, the application need to pick the properties file according to the environment. 
So, at the top of the  QuartzSchedulerApplication class, add this configuration annotation.

 @Configuration("classpath:application-" + "${spring.profiles.active}" + ".properties")

spring boot profiles example


If no profile is mentioned, the default profile will be default.

While running the application, specify the profile property like below.

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=<env> 


Persisting Data to H2 with spring boot


  • H2 in memory open source in memory DB written Java.
  • It doesn't  persist data to  disk, so it is not recommended to use for production environment. 
  • Good for POC's, DEV environments.
To use H2 in your project, include this dependency in the pom.xml file.


ORM (Object Relation mapping) with JPA (Java persistence API)


  • ORM means mapping Java objects to relations(Database objects) and JPA is an interface with empty methods that describes Java persistence methodologies to do so.
  • In order to be functional, JPA needs implementation. and Hibernate provides implementations.
  • Spring data JPA provides repository support for the JPA. It eases the development of applications that need to access JPA Data sources.
  •  Spring data JPA (spring-boot -starter-data-pa) provides following 
    • Hibernate (Popular JPA Implementation) 
    • Spring data JPA 
    • Spring ORM

Entities

  • Entities are objects that live in database & have the ability be mapped to Database tables. 
  • Entities are defined by @Entity Annotation Traditionally. 
  • JPA entity classes are specified in persistence.xml file. 
  • In Spring Boot, this is not necessary. Entity Scanning is used. All packages under class with @SpringBootApplication @EnableAllConfiguration annotations are searched.

REST Architectural style


Representational State transfer is  a set of guidelines that application developer use to design APIs There are 4 principles APIs follow. They are

  • Data & functionality are considered resources and Identified through URI (Uniform Resource identifier) which are accessed by weblinks. 
  • Resources are manipulated by a fixed set of operations a. GET-retrieves a resources b. Past - creates a resource c. PUT updates a resource d. DELETE- removes a resource 
  • Resources can be represented in multiple formats XML, HTML, JSON etc defined by a media Type. 
  • Communication between client & endpoint is stateless. Server will not remember state or store any state from client that made the call.

 Let us create a spring boot application and create rest APIs using spring boot. 

Refer below spring boot tutorial posts to create a java spring boot application with working code base. 



Post a Comment