Tuesday 6 February 2018

Spring Boot

Spring Boot is a framework which have Auto Configuration, Auto Dependency Resolution and Embedded HTTP Server like Tomcat and Jetty.

Spring Boot main aim is "To simplify the Spring Based Application Development".

To Start the Spring Boot Application below Jars are required.
  • SpringBootStarterWeb
  • SpringBootStarterParent
  • Java8Version
  • SpringBoot Maven Plugin.
Pros : 
  • No need to write XML Configurations, all configurations we can write in java code.
  • Reduces the Boilerplate code.
  • Testing is very easy.
  • Quick starts of application.
  • Provides all non functional features like Embedded Server, Security, Matrix and healthchecks.
  • Provides lot of plugin to work with Embedded Systems.
  • Provides Command Line Interface (CLI) Tool for development and testing.
Cons : 
  • Very tough to convert the legacy projects into new Projects.
Spring Boot Jars & uses : 
  • Spring-Boot-Starter-Web-Service : We can work with SOAP Web Services.
  • Spring-Boot-Starter-test : For Junit and MOCITO etc.
  • Spring-Boot-Starter-jdbc : For Spring JDBC
  • Spring-Boot-Starter-Security : Spring Security
  • Spring-Boot-Starter-data-JPA : JPA
  • Spring-Boot-Starter-data-rest : Restful WebServices.
  • Spring-Boot-Actuator : To load the Matrices and Project Health checkups.
More Points About Spring Boot : 

  • Spring Boot Starter Web Jar : It includes Tomcat, Hibernate, Restful Service, Logging and Web jars.
  • Spring Boot Auto Configuration : It will have Auto mated Error pages. Automatically Error page will come if we hit wrong URL. 
  • @ Spring Boot Application : Will include @EnableAutoConfiguration, @Configuration, @ComponentScan.
@SpringBootApplication :

@Configuration : Denotes the bean definitions for application context.
@EnableAutoConfiguration : It enables the bean presents in class path settings and various property settings.
@ComponentScan : It tells to spring scan all the components like configurations and services and other components in the application.

Spring-boot-starter-cache
Enabling spring framework Caching support.
@EnableCache :
Load Changes without Restart the Server
  • By using Spring-boot-devtools Dependency we can load the changes and it will reflect without restarting the server.
Spring-Boot-Actuator
  • It uses to load the Matrics and Health Checkups for application.
  • Basically it will uses in Production environment only.
  • By using " Spring-Boot-Actuator" Jar we can use this service.
Run with Custom Port : 
  • Add the following port in application.properties file
server.port = 8091

Spring Boot Remote Application :
  • By using below configuration, we can run the Spring Boot Application Remotely.
spring.devtools.remote.sercret = mysecret
Validations in Spring Boot :
  • By using below annotations we can validate 
@ConfigurationProperties , @Value 
Spring-Boot-Starter-Quartz
  • We can schedule the cron jobs by using Spring-Boot-Starter-Quartz.
HIKARI Connection Pool in Spring Boot :
Step 1 : Add a "hikaricp 2.6.1 jar" or "latest hikari jar" as dependency in pom.xml.
Step 2 : Add a below property in application. properties file.
spring.datasource.type = com.zaxxer.hikari.HikariDataSource
Step 3 : Set all Hikari properties in properties file like timeout etc.
Step 4 : Use HikariDataSource.
Step 5 : Set all the properties in util class or configuration class.
             private static HikariDataSource ds = new HikariDataSource();
             ds.setDriverName("");
             ds.setJdbcUrl("");
             ds.setUserName("");
             ds.setPassword("");

Proc of HikarCP
  • Its very faster and zero overhead production quality connection pool.
Override the Embedded server in Spring-boot :
Step 1 : Exclude the tomcat dependency in Spring-boot-starter-web by using below exclusion.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>



Step 2 : Change the default server port number.
Step 3 : Change the Server-specific properties.(SSL, ContextPath, ContextParameter etc.)

How to provide security in Spring Boot :
Use Spring-Boot-Starter-Security in pom.xml and in configuration file extends the WebSecurityConfigurerProperty class and override the methods.

How to provide the security for REST URL or URI or API :
In Different ways we can provide the Security for Rest API's : 
  1. Basic authentication (SSL Certificate) : Client will provide the SSL Certificate and install it in Web Server.
  2. OAUTH 1.0a : OAUTH is signature based protocol.  This protocol uses Cryptography signature. This can be used without SSL.
  3. OAUTH 2 : This protocol completely eliminates the signature. It requires Transport layer security which handles the encryption. This is very simple to configure so this will use for less sensitive data.
Because of above adv and disadvantages it is safer and simpler to choose basic Auth with SSL Cert for Most REST Services

ActiveMQ in SpringBoot :
By using "Spring-boot-starter-activemq" jar in pom.xml.

In Spring Boot How it will take Hibernate as default implementation of JPA :
First Need to add Spring-Boot-Starter-Data-JPA dependency in pom.xml file.

Once Spring Boot application starts it will check the class-path. If Hibernate dependency in the class path then Spring boot will take as default implementation of JPA. If it is H2 database related Jars in class-path then we can write the H2 implementation for JPA. 

How do we connect the external database in Spring Boot :
Remove H2 database jar or Hibernate jar in pom.xml or make it SCOPE as test. Add the Other external Database jar (MySql or Oracle) into pom.xml and configure the properties in Configuration class file and add the properties in property file.

Profile In Spring Boot
Enterprise applications will have multiple environments like DEV, STAGE, PROD.
So Profile will help to have different application configuration for different environments.
Profile Configuration :
We are having 2 profiles : Dev and Prod
Step 1 : Override the property file name like below
Dev : application-dev.properties
Prod : application-prod.properties

Step 2: Once we have a specific profile configuration we have to set the Active profile.
2 way we can do this. 
1. By changing the property in applicaiton.properties file.
spring.profile.active = prod
2. By changing the arguments in environment.
-Dspring.profile.active = prod


More Points will be add soon...

No comments:

Post a Comment

SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...