Monday, June 1, 2020

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade spring based applications that you can "just run".

What is Spring?

  • Application framework
  • Programming and configuration model
  • Infrastructure support

Problem with Spring

  • Huge framework
  • Multiple setup steps
  • Multiple configuration steps
  • Multiple build and deploy steps

Why do we need Spring Boot?

  • Due to the problems in spring framework, spring boot comes.
  • Opinionated
  • Convention over configuration
  • Stand alone
  • Production ready

Spring Boot Features

Configuration and Customization

Lecture -7 (Maven):

  • Maven is a project management tool that is based on project object model (POM). It is used for project build, dependency, and documentation. In short, maven is a tool that can be used for building and managing any java based project.
  • Maven lets you declare all the dependencies that you want in a single file, you do not have to download the jars and add it to your class path, you just create pom.xml and list dependencies there and run maven command. This file contains a list of all your dependencies that maven (dependency tool management) needs to know.

Starting spring Boot

  • Sets up default configuration
  • Starts Spring application context
  • Performs class path scan
  • Starts Tomcat server

Controller:

A controller is basically a java class that has certain annotations marked. This annotation lets spring know what is the URL that you are mapping into and what should happen when the request comes to that URL.

What are differences between spring and spring boot? (interview question)

SpringSpring Boot
  • Configuration: To design any spring based application, the developer has to take recourse to the annual setup feature on the hibernate data source. Session Factory, entity Manager, Transaction Management, etc. have to be configured as well.
  • XML: In Spring MVC applications, some XML definitions are to be managed mandatorily.
  • This is injection framework
  • Takes time to have spring application up and running
  • A web application framework based on java
  • Provides tools and libraries to create customized web application
  • Spring is more complex than spring boot
  • Takes an opinionated view
  • Configuration: The standard set up and features of Spring Boot do not have to be designed by the developer individually. The Spring Boot Configuration annotation is well-equipped to handle everything at the time of deployment.
  • XML: In the configuration of Spring Boot applications, nothing has to be managed manually. The annotations are capable of handling all that is needed.
  • Pre-configured set of frameworks/technologies
  • Shortest way to run spring application
  • A module of java
  • Used to create a spring application project which can just run/execute
  • Spring boot is less complex than spring framework
  • Takes an opinionated view of a platform

What is Spring Boot and mention the need for it?

Spring Boot is a Spring module which aims to simplify the use of the spring framework for java development.
Spring Boot framework comes with
  • Auto dependency Resolution
  • Embedded HTTP servers
  • Auto configuration
  • Management endpoints
  • spring Boot CLI

Advantages of Spring Boot

  1. Provides autoconfiguration to load a set of default configuration for a quick start of the application
  2. Creates stand alone applications with a range of non-functional features that are common to large classes of project
  3. Spring boot comes with embedded tomcat, servlet containers to avoid the usage of WAR files
  4. Spring Boot provides an opinionated view to reduce the developer effort and simplify maven configuration
  5. Provides CLI tool to develop and test applications
  6. Comes with Spring Boot starters to ensure dependency management and also provides various security metrics
  7. Consists of a wide range of APIs for monitoring and managing applications in dev and prod
  8. Integrates with Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security easily by avoiding boilerplate code

How to create Spring Boot application using Maven?

There are various approaches to create a Spring Boot application using maven:
  • Spring Boot CLI
  • Spring Boot Project Wizard
  • Spring Boot Initializer
  • Spring Maven Project

What are possible sources of external configuration?

The most possible source of external configuration are:
  1. Application Properties
  2. Command Line Properties
  3. Profile Specific Properties

What are the Spring Boot starters?

  1. spring-boot-starter
  2. spring-boot-starter-jdbc
  3. spring-boot-starter-web
  4. spring-boot-starter-data-jpa
  5. spring-boot-starter-security
  6. spring-boot-starter-aop
  7. spring-boot-starter-test

Explain Spring Actuator and its advantages

Spring Actuator is a cool feature of Spring Boot with the help of which you can see what is happening inside a running application.
  • The Spring Actuator provides a very easy way to access the production-ready REST points and fetch all kinds of information from the web
  • These points are secured using spring security's content negotiation strategy

What is Spring Boot dependency management?

Spring Boot dependency management is used to manage dependencies and configuration automatically without you specifying the version for any of that dependencies.

1. RestTemplate class

Accessing a third-party REST service inside a Spring application revolves around the use of the Spring RestTemplate class. The RestTemplate class is designed on the same principles as the many other Spring *Template classes (e.g., JdbcTemplateJmsTemplate ), providing a simplified approach with default behaviors for performing complex tasks. RestTemplate class is designed to call REST services.

2. Spring RestTemplate – HTTP GET Method Example

  • getForObject(url, classType) – retrieve a representation by doing a GET on the URL. The response (if any) is unmarshalled to given class type and returned.
  • getForEntity(url, responseType) – retrieve a representation as ResponseEntity by doing a GET on the URL.
  • exchange(requestEntity, responseType) – execute the specified request and return the response as ResponseEntity.
  • execute(url, httpMethod, requestCallback, responseExtractor) – execute the httpMethod to the given URI template, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.


Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods. 

No comments:

Post a Comment