JAVA Spring-Boot Framework

Essential Features of Spring Boot 


What is Spring Boot


Create stand-alone Java applications, which contains an embedded tomcat server. It is a Spring-based production-ready project initializer. It reduces the requirement to write a lot of configuration and boilerplate code.


1. AutoConfiguration

In a modern-day Spring application, which uses Java-based configuration, you need to add the following two methods into your Configuration class

@Bean
 public JdbcTemplate jdbcTempalte(DateSource ds){
      return new JdbcTempalte(ds); 


@Bean 
public DataSource dataSource(){
  return new EmbeddedDatabaseBuilder()
 .setType(EmbeddedDatabaseType.H2) 
 .addScripts('ddl.sql', 'data.sql') 
.build(); 
}            


But, more importantly, this is a piece of code which many of us have written irrespective of our application. I mean, this code is not unique and every single Spring application which works with JDBC will need it.

That's where Spring Boot AutoConfiguration comes into play. It detects the presence of certain Class in the Classpath and then automatically configure it for you.

For example, if you have added JdbcTempalte into classpath and also H2.jar then Spring Boot can automatically configure an in-memory database and a JdbcTempatle which is ready to use.No don't need to write above code to use JdbcTemplate in your DAO layer.

The Auto-Configuration feature is by default disabled and you need to enable it by using @EnableAutoConfiguration or @SpringBootApplication annotation on your Configuration class. I normally annotated the Main class, which I am going to run with an embedded Tomcat server.

It's recommended to use @SpringBootApplication annotation from Spring Boot 1.2 onwards as it combines a couple of other annotations to make your code more readable


2. Starter POMs


The Starter POMs take away the difficulties by finding and adding common dependencies in your project.

In order to build a simple Spring MVC based REST application which supports Jackson and to run it an embedded container, you would at least need following dependencies e.g.

spring-core.jar
spring-web.jar
spring-webmvc.jar
jackson-databind.jar
tomcat-embed-core.jar
tomcat-embed-el.jar
tomcat-embed-logging-juil.jar

By using Spring Boot Starter POMs or starter dependency feature, you can get all of these by just adding spring-boot-starter-web dependency in your pom.xml

Starter POMs feature works internally because of the Maven or Gradle's transitive dependency feature.

 It's Maven or Gradle which pulls the right version of libraries, Starter POMs just declare them.


3. Spring Boot CLI


The Spring Boot CLI is a command line interface provided by Spring Boot framework which allows you to create a Spring-based web application using Groovy programming language.

Actually, Groovy and Spring Boot nicely complement each other, Groovy aims to make Java development simpler while Spring Boot aims to make Spring application development simpler and both benefit from each other's simplicity.

simple HelloWorld RESTful Web Service in Groovy and Spring Boot CLI and it works.

@RestController
class HelloSpringBootController{

  @RequestMapping("/")
  def hello() {
    return "Hello Spring Boot CLI"
   }
}

That's it, you can run it on an embedded container which comes with Spring Boot CLI, no web.xml, no configuration, and no server setup.

Spring Boot CLI detect that @RestController and @RequestMapping are in use and it knows which starter dependencies are required to add into classpath to make it work.

Once it downloads those series of dependencies, auto-configuration automatically kicks-in and configured it for use e.g. once spring-boot-web-starter comes into the picture it downloads spring-mvc.jar and then auto-configuration automatically configure DispatcherServlet and enable Spring MVC

Spring Boot Security mechanisms and OAuth2 with JWT


Authorization Server

Authorization Server is a supreme architectural component for Web API Security. The Authorization Server acts a centralization authorization point that allows your apps and HTTP endpoints to identify the features of your application

Resource Server

Resource Server is an application that provides the access token to the clients to access the Resource Server HTTP Endpoints. It is a collection of libraries which contains the HTTP Endpoints, static resources, and Dynamic web pages

OAuth2

OAuth2 is an authorization framework that enables the application of Web Security to access the resources from the client. To build an OAuth2 application, we need to focus on the Grant Type (Authorization code), Client ID and Client secret

JWT Token

JWT Token is a JSON Web Token, used to represent the claims secured between two parties.

Now, we are going to build an OAuth2 application that enables the use of Authorization Server, Resource Server with the help of a JWT Token.

You can use the following steps to implement the Spring Boot Security with JWT token by accessing the database.

First, we need to add the following dependencies in our build configuration file.

Maven users can add the following dependencies in your pom.xml file.




Implementation of Features

Spring Boot Starter Security − Implements the Spring Security

Spring Security OAuth2 − Implements the OAUTH2 structure to enable the Authorization Server and Resource Server.

Spring Security JWT − Generates the JWT Token for Web security

Spring Boot Starter JDBC − Accesses the database to ensure the user is available or not.

Spring Boot Starter Web − Writes HTTP endpoints.

H2 Database − Stores the user information for authentication and authorization.

Beans and Dependency Injection


In Spring Boot, we can use the Spring Framework to define our beans and their dependency injection. The @ComponentScan annotation is used to find beans and the corresponding injected with @Autowired annotation.

If you followed the Spring Boot typical layout, no need to specify any arguments for @ComponentScan annotation. All component class files are automatically registered with Spring Beans

Comments

Popular posts from this blog

Mongo DB - NoSQL

How Javascript is working-Mechanisms

Creating a REACT component