TechWriterDev
  • Cloud
    • AWS
      • 00_Doubts
      • CloudPractitioner
        • Cloud Computing
        • AWS Global Infrastructure
        • Introduction to AWS EC2
        • Elastic load balancer(ELB)
        • 04_Messaging_Queuing
        • Aditional Computing Service
        • Accessing AWS resources
        • AWS Networking
        • Storage
        • Amazon Database Solutions
        • Monitoring Tools
        • AWS Security
        • Distributed Denial Of Service Attacks
      • DeveloperAssociate
        • References
        • AWS DVA-C02 Services Index
        • Services
          • 00_IAM
            • Identity and Access Management (IAM)
            • Account Protection Mechanisms
            • Access Mechanism of AWS Resources
            • Security Tools
            • Responsibility Model
            • Advanced Concepts
          • 01_EC2
            • Elastic Compute Cloud (EC2)
            • EC2 Volume Types
            • Amazon Machine Image (AMI)
            • AWS charges for IPv4 address
          • 02_SecurityGroups
            • Security Groups
          • 03_Elastic_LoadBalancing
            • Terminology
            • Elastic load balancer
            • Features
            • Basics
          • 04_AutoScaling
            • Auto Scaling
          • 05_RDS
            • Relational Database Service (RDS)
            • Aurora
            • Security
            • RDS Proxy
          • 06_ElastiCache
            • Cache
            • Cache Offerings
          • 07_Route53
            • Basics of DNS
            • Route 53
          • 08_VPC
            • Virtual Private Cloud (VPC)
          • 09_S3
            • Simple Storage Service (S3)
            • S3 Features
            • S3 Encryption
            • S3 Features
            • S3 Bucket Policy and IAM Policy
          • 10_ECS
            • Elastic Container Service (ECS)
            • Elastic Container Registry (ECR)
            • AWS Copilot
          • 11_EKS
            • Elastic Kubernetes Service (EKS)
          • 12_SDK_CLI_Tips
            • Access AWS Resources
          • 13_CloudFront
            • Cloud Front
          • 14_Messaging
            • Simple Queue Service (SQS)
            • Simple Notification Service (SNS)
            • Fan Out Pattern
            • Kinesis
            • Compare and Contrast
          • 15_ElasticBeanStalk
            • Elastic Beanstalk
          • 16_CloudFormation
            • CloudFormation
            • Dynamic References
          • 17_Monitoring
            • AWS Monitoring
            • AWS CloudWatch
            • CloudWatch Alarms
            • Synthetics Canary
            • Amazon EventBridge (formerly CloudWatch Events)
            • X-Ray
            • OpenTelemetry
            • CloudTrail
          • 18_Lambda
            • Lambda
            • Lambda Integrations
            • Configuring Lambda
            • Lambda Layers
          • 19_API_Gateway
            • API Gateway
            • API Gateway Integrations
          • 20_DynamoDB
            • DynamoDB
            • Operations
            • Indexes
            • DynamoDB Accelerator (DAX)
            • DynamoDB Streams
            • Transactions
            • Integrations
          • 21_CICD
            • CICD
            • CodeCommit
            • CodePipeline
            • CodeBuild
            • CodeDeploy
            • CodeArtifact
            • CloudGuru
          • 22_SAM
            • Serverless Application Model (SAM)
          • 23_CDK
            • Cloud Development Kit (CDK)
          • 24_StepFunctions
            • Step Functions
            • Types of step function
          • 25_AppSync
            • AppSync
          • 26_Amplify
            • Amplify
          • 27_STS
            • Security Token Service (STS)
          • 28_DirectoryService
            • Active Directory
          • 29_KMS
            • Encryption
            • KMS API
            • Features
            • Cloud Hardware Security Module (HSM)
          • 30_SSM_Store
            • SSM Parameter Store
          • 31_SecretsManager
            • Secrets Manager
          • 32_Cognito
            • Cognito
      • Questions
        • AWS_Region
        • EC2
        • IAM
  • Database
    • MongoDb
      • Mongo db Basics
      • Mongo DB Atlas
      • Document
      • Import-Export based on Data Format
      • Mongo Shell Commands
      • Query Operators
      • Indexes
      • Upsert
      • MongoDB Aggregation Framework
      • Aggregation Framework Operators
    • PostgreSQL
      • POSTGRE SQL DataTypes
      • About table
      • Constraints
  • Technologies
    • RabbitMQ
      • RabbitMQ Concepts
      • Introduction to Exchanges
      • Introduction to Queues
    • Terraform
      • 00_Introduction
      • Configuration blocks
      • Commands
      • Variables
      • Terraform Cloud
      • Modules
  • Languages
    • Java
      • Logging
        • Getting Started
      • 00_Core
        • 00_Basics
          • Java Vs C++
          • Object oriented principles
          • Steps to compile a java program
          • JVM Internals
          • Understanding Java Development Kit
          • What is JIT Compiler?
          • Java data types
          • 07_identifiers_type_conversion
          • 08_references_and_packages
          • Steps for attaching scanner
        • Concurrency
          • 00_Threads
            • Threads
          • 01_ExecutorFramework
            • Executor Framework
            • Asynchronous Computation
      • 01_Backend
        • 01_HttpAndWebServerBasics
          • HTTP
          • Content Type
          • Web Server
        • 02_J2EE_Basics
          • J2EE_Basics
          • Why HttpServlet classs is declared as abstract class BUT with 100 % concrete functionality ?
        • 03_TomCatAndSession
          • What is a Session?
          • WebContainer
        • 04_PageNavigation
          • Cookies Additional Information
          • Page Navigation Techniques
        • 05_AboutServlet
          • CGI v/s Servlet
          • Executor Framework
          • Servlet Life cycle
          • SERVLET CONFIG
          • Servlet Context
          • Servlet Listener (web application listener)
        • 08_SpringBoot
          • Spring Boot
          • Some common annotations used in spring eco system
        • 09_SpringDataJPA
          • Spring Data JPA
        • Java_Language_Changes
          • JDK enhancement tracking reference
        • 06_ORM_Hibernate
          • readmes
            • Hibernate
            • Advantages of Hibernate
            • Hibernate Caching
            • Hibernate API
            • Hibernate Query API
            • Hibernate Annotations and JPQL
            • Entity and Value Type
        • 07_SpringFramework
          • bean_validation
            • Bean Validation
          • core
            • readme
              • Spring
              • Spring Framework Modules
              • Spring MVC Request flow
              • Dependency Injection
              • Spring Beans
              • 06_Spring_Framework_Annotations
      • 03_Tools
        • Maven
          • Maven
  • SoftwareEngineering
    • DesignPatterns
      • Notes
        • Basics
        • OOP
        • SOLID Principles
        • 03_Creational
          • Abstract Factory (aka Kit)
          • Builder
          • Factory Method (aka Virtual constructor)
          • Prototype
          • Singleton
        • 04_Structural
          • Adapter (aka Wrapper)
          • Bridge (aka Handle | Body)
          • Composite
          • Decorator (aka Wrapper)
          • Facade
          • Flyweight
          • Proxy (aka Surrogate)
        • 05_Behavioral
          • Chain of Responsibility
          • Command (aka Action | Transaction)
          • Iterator (aka Cursor)
          • Observer (aka Publish-Subscribe | Dependents)
          • Strategy (aka Policy)
    • Principles
      • REST
        • REST
  • Tools
    • Containers
      • Docker
        • Docker
        • Docker Image
        • Commands
        • Compose
        • Best Practices
      • Kubernetes
        • Kubernetes
    • VCS
      • Git
        • Quick reference of useful Git commands
Powered by GitBook
On this page
  • Problems with traditional Spring Application
  • Spring boot
  • Important Annotations of a Spring Boot Application
  • Advantages of Spring Boot
  • What is "Opinionated Defaults Configuration"
  • How does @EnableAutoConfiguration work?
  • What is Spring Boot Starter?
  1. Languages
  2. Java
  3. 01_Backend
  4. 08_SpringBoot

Spring Boot

Previous08_SpringBootNextSome common annotations used in spring eco system

Last updated 4 months ago

Problems with traditional Spring Application

  • We use different modules from spring such as,

    • Core module, to do dependency injection.

    • The MVC module to develop the web layer for our application or even the restful web services layer.

    • The DAO layer where we use the spring JDBC/ORM which makes our life easy to develop a data access layer for our application.

    • While using ORM tools like Hibernate, we can use spring data JPA and we use these modules and more, that are valuable from Spring.

  • Configuration

    • We used XML based configuration or annotations based configuration. This configuration can get difficult and hard to maintain over time.

    • And also we need to make sure that each of these modules is available for our application by defining all the dependencies in the Maven POM.

    • And at runtime we have to be sure that these versions of various Modules that we use are compatible with each other.

    • It's our responsibility to do all that, and once we have all that in place we will build our application and will have to deploy the application to an external web container to test it.

  • A brief history of Spring framework evolution can be found .

Spring boot

  • This raised for creating Spring Boot, which changed how spring applications are developed.

  • What is Spring Boot?

    • Spring Boot is a Framework from "The Spring Team" to ease the bootstrapping and development of new Spring applications.

    • Spring Boot is a production ready spring project with embedded web server and configuration files.

    • It provides defaults for code and annotation configuration to quick start new Spring projects within no time. Spring projects required quite a bit of configuration. Some of this configurations are boilerplate code related to persistence, file upload etc. It has nothing to do with business requirements.

    • Spring Boot is a framework that will set up much of this configuration automatically, helping you get your project up and running as quickly as possible.

    • It follows Opinionated Defaults Configuration approach to avoid lot of boilerplate code and configuration to improve Development, Unit Test and Integration Test Process.

  • What are Spring Boot Features?

    1. Auto configuration : Spring Boot automatically configures everything that is required for our application. We don't have to use XML or annotation based or Java configuration anymore.

      • For example, if you are using Spring MVC or the web to develop a web application or a restful web service application spring boot will automatically configure the dispatcher servlet and does all the request mapping for us. We don't have to use any xml or annotation based configuration to configure dispatcher servlet.

      • Similarly, if you are using spring data for object relational mapping while working with tools like Hibernate to perform database CRUD operations, we no longer have to configure the data source or even the transaction manager. Spring boot will automatically configure these for our application.

    2. Spring boot starters : With the spring boot starters spring boot solved the problem of module availability we need. Before Spring Boot we had to make sure that a particular library required for our project is available and also the versions of different libraries are compatible. But we don't have to do that anymore, thanks to spring boot starters. Every spring boot project will have a parent project. This project has all the version information of various libraries that we will be using in our project so we need not worry about version compatibility. The spring developers have already done it for us and put all that information into this spring boot starter parent.

      • Secondly, we have starters for different types of projects. If we are developing a web project then we simply need to include the starter web dependency. We don't have to include any other libraries or dependencies. Maven (or any dependency management tool) will automatically pull those libraries. The spring boot developers have given us starter dependencies which when we use in our projects will not have the modular availability problem and the version compatibility problem.

      • Another example is, the spring boot starter data jpa. When you want to work with Hibernate you simply include the single dependency in your maven pom.xml and all the other libraries will be pulled accordingly. And also the correct versions of those libraries will be included because all that version information is available in this spring boot starter parent which is a parent for every spring boot project.

    3. Embedded servlet container: Deploying our applications to external container becomes easy as spring boot comes with an embedded servlet container.

      • By default, it is Tomcat but you can also use Jetty and Undertow or any other external server. So, no longer you have to worry about external deployment. You can simply right click on your project and run it and your application will be launched on its embedded Tomcat server by default.

    4. Health checks : Spring Boot Actuators enable easy health checks for our application. We can use different types of health checks that come for free and we can use these even on production when our application is running. These can be activated easily and will display all the auto configuration reports and everything that is automatically configured for our application.

Important Annotations of a Spring Boot Application

  • Entry point

        @SpringBootApplication
        public class HellospringbootApplication { 
    
            public static void main(String[] args) {
                SpringApplication.run(HelloSpringBootApplication.class, args);
            }
        }
    • org.springframework.boot.SpringApplication class is used to bootstrap and launch a Spring application from a Java main method.

    • By default, this class will perform the following steps to bootstrap the application,

      1. Create an ApplicationContext instance (representing Spring Container).

      2. Manages life-cycle of spring beans.

    • @SpringBootApplication

      • This annotation is where all the spring boot magic happens.

      • It is a key annotation which is a top level annotation which contains several other annotations on it such as,

        • org.springframework.boot.@SpringBootConfiguration : Tells spring boot or the container that this class here can have several bean definitions. We can define various spring beans here and those beans will be available at run time if you define a method with @Bean annotation here.

        • org.springframework.boot.autoconfigure.@EnableAutoConfiguration : It is a very important annotation that enable Auto configuration. This annotation tells spring boot to automatically configure the spring application based on the dependencies that it sees on the classpath.

          • For example, if you have a MySql dependency in your pom.xml, then Spring Boot will automatically create a data source. You will also provide other information like username, password etc. but spring boot will scan through all these dependencies and it will automatically configure the data source required for you.

          • Another example is spring web, if you have spring web in your dependencies. Then spring boot will automatically create the dispatcher servlet.

          • All the xml, all the java based configuration is now gone. Developers need not do all that configuration, as all that comes for free thanks to spring boot because of its @EnableAutoConfiguration annotation.

        • org.springframework.context.annotation.@ComponentScan : This tells that spring boot or spring should scan through the classes and see which all classes are marked with the stereotype annotations like @Component, @Controller, @Service, @Repository and manage these spring beans .

          • Note: Default base package is the package in which Main class is defined. However, this can be overridden by providing annotation property basePackages.

            • eg

                @ComponentScan(basePackages = "com")
          • This annotation is equivalent to xml tag <context:component-scan>.

    • For scanning entities use @EntityScan.

      • This annotation is used to scan entity classes, which are in a different root package hierarchy than Main class.

          @EntityScan(basePackages = "com.app.pojos")
      • Using this annotation will disable the auto configuration capability of @EnableAutoConfiguration for entity classes.

Advantages of Spring Boot

  • It makes development of Spring Based applications with Java very easy.

  • It reduces lots of development time and increases productivity.

  • It reduces writing lots of boilerplate Code, Annotations and XML Configuration.

  • It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.

  • It follows “Opinionated Defaults Configuration” Approach to reduce Developer effort.

  • It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.

  • It provides CLI (Command Line Interface) tool to develop and test Spring Boot (Java or Groovy) Applications from command prompt very easily and quickly.

  • It provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle.

  • It provides lots of plugins to work with embedded and in-memory Databases (like H2 database) very easily.

What is "Opinionated Defaults Configuration"

  • When we use Hibernate/JPA, we would need to configure a datasource, a session factory, a transaction manager among lot of other things.

  • Spring Boot looks at it differently.

    • By auto-configuring a datasource i.e., connection pool/session factory/Transaction manager, if Hibernate jar is on the classpath.

    • When a spring mvc jar is added into an application, spring auto configure some beans automatically such as HandlerMapping, ViewResolver and configure DispatcherServlet.

  • Spring, of course allow and provision ways to override the default auto configuration.

How does @EnableAutoConfiguration work?

  • Spring Boot looks at jars available on the CLASSPATH and existing configuration for the application.

  • Based on these, Spring Boot provides basic configuration needed to configure the application with these frameworks. This is called Auto Configuration.

What is Spring Boot Starter?

  • Starters are a set of convenient dependency descriptors that you can include in your application's pom.xml.

  • For example, suppose you want to develop a web application, without Spring boot, you would need to identify the frameworks we want to use, which versions of frameworks to use and how to connect them together.

  • But all web application have similar needs.

    • These include Spring MVC, Jackson Databind (for data binding), Hibernate-Validator (for server side validation using Java Validation API) and Log4j (for logging).

    • Earlier while creating any web app, we had to choose the compatible versions of all these frameworks.

  • With Spring boot, You just add Spring Boot Starter Web.

        //Dependency for Spring Boot Starter Web
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • Just by adding above starter, it will add necessary and compatible JARs under maven dependencies.

  • Another example, if you want to use Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.

here
ticket