"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Introduction To SpringBoot Date : 28/03/2025 (Session - 14) _____________________________________________________________________________________________________________________________ Today Session : Spring Boot =========================== * Spring boot is an open source java based framework developed by pivotal team/spring team for developing spring applications in easier way. * Spring boot is not sub module of Spring Framework, It is another Project from Pivotal Team / Spring Team. * Spring boot is not replacement of Spring Framework, It is extension of Spring Framework. Spring Boot = SpringFramework++ * Spring boot is developed on top of Spring Framework, What ever features we learned in spring framework it will works in spring boot as well. * Spring Boot is used to build Standalone Application and Production Ready Features for Spring Application Production Ready >> Spring Boot includes no of additional features to help you monitor and manage the Applications when its pushed to production. >> We can monitor the Application by using Http end points (or) JMX. >> Auditing,Health,Metrics gathering can also be automatically applied to your application. >> By using Actuators we can develop the Production ready features Spring Boot = Spring Framework - XML Configuration Features of Spring Boot ======================= 1) Create standalone Spring applications 2) Embed Tomcat(default), Jetty, Undertow directly (no need to deploy WAR files) >> Embedded Servers 3) Provide opinionated 'starter' dependencies to simplify your build configuration >> Starter Dependencies Starter Dependencies >>>> It is special type of POM and it can be download dependencies required for project. Every Starter in spring boot will have some common prefix i.e.,spring-boot-starter-*. Spring boot provides some of starters below i) Spring-boot-starter-web >>>>>>> To Download dependencies(Jars) required for the Web/Distributed Application Development. ii) spring-boot-starter-data-jdbc >>>>>>> It will download dependencies(Jars) required for developing persistence logic using Jdbc iii) spring-boot-starter-data-jpa >>>>>> It will download dependencies(Jars) required for developing persistence logic using ORM. Fore More Starters information : ================================ https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.starters 4) Automatically configure Spring and 3rd party libraries whenever possible >> Auto Configuration Auto Configuration >>>> Automatically doing configuration of certain java classes as Spring Beans. Traditional Spring Application Development ========================================== * As a Programmer we need to certain java class as spring bean in following approaches i) XML configuration ii) Java Based Configuration iii) Annotaton Configuration * Whenever we are developing webapplication using Spring MVC Module as aprogrammer we need to do the following things i) Configuring the DispatcherServlet in web.xml ii) Enabling the load on startup for DispatcherServlet by Spring IOC container iii) We need to provide the location of Spring Configuration file. * In Spring Boot MVC Development we no need to do above configuration because Spring boot having feature "Auto Configuration" through the DispatcherServlet will as spring bean by default adding following starter i.e.,spring- boot-starter-web similary to this one JdbcTemplate,NamedParameterJdbcTemplate will becomes as spring bean automatically when you include following starter i.e.,spring-boot-starter-data-jdbc. 5) Provide production-ready features such as metrics, health checks, and externalized configuration >> production ready features 6) Absolutely no code generation and no requirement for XML configuration >> No XML Configuration 7) Spring Boot Application also supports Embedded Databases /In-Memory Databases. 8) Spring Boot Application automatically container will be activated based on starter Selection. 9) Version Management NOTE ==== Embedded Server >> The server which comes along with Spring Boot Application called Emebedded Servers Eg: Tomcat (default) Embedded database >> The Database which comes along with Spring boot Application called "In-Memory Databases" Eg: h2 Starter Dependencies >> Special type of Dependencies managed in pom.xml AutoConfiguration >> When you add the starters to Spring boot automatically certain java class will becomes Spring beans and Performs Dependency Injection as well. 100% XML Configuration is not required "Spring Boot = Spring Framework + Embedded Servers +Embedded Databases - Xml Configuration" Advantages of Using Spring Boot =============================== * It is very easy to develop the Spring Based Application using Java (or) Groovy (or) Kotlin. * It reduces lot of development time and increases Productivity. * It avoids 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 "opnionated Default Configuration" approach to reduce developer effort. * It provides Embedded Http Servers like Tomcat,Jetty,undertow, to develop and test our webapplication very easily. * Spring Boot Provides CLI(Command Line Interface) tool inorder to develop and test the application very easily. * It provide supports of various build tools(Maven,Gradle) for developing spring boot application * It provides supports of working with embedded in-memory databases very easily. * By using Spring Boot we can develop the REST API's very easily. * Spring Boot Supports for developing the MicroServices Architecture based Applications. Disadvantages of Spring Boot ============================ * It is very tough and time consuming process to convert existing Spring Application (or) legacy Application into Spring Boot Application. * It is recommended to use for Brand-New Projects. Spring Vs Spring Boot ===================== Spring ====== 1) Spring is an application framework for developing the different types applications. 2) Spring provides an abstraction layer on Java,J2EE Technologies. 3) Spring Framework main feature is Dependency Injection & loose coupling between Java Objects. 4) Spring Framework XML Driven Configuration to provide Inputs/Configuration to IOC Container. 5) Spring Framework allows to develop application using three approaches * XML driven Configuration * Annotation Driven Configuration * Java Based Driven Configuration 6) In Spring Framework as programmer we need to create the container object explictly. 7) Spring Framework doesn't given Embedded Server...so running the webapplication we need to arrange server explictly. 8) Spring Framework doesn't given any InMemory Databases definelty we need to go for external database configuration. 9) We need to add depedencies required for project in pom.xml explictly. 10) Spring Framework doesn't support for Microservices Architeture based application developement. 11) Spring Framework doesn't have any built-in support of properties file programmer need to configure explictly. 12) Spring Framework doesn't have support of YAML File. Spring Boot =========== 1) Spring Boot is another style for developing the spring application. 2) Spring Boot provides abstraction on Spring Framework and its simplified away Spring application development. 3) Spring Boot Main feature is "AutoConfiguration". 4) Spring Boot Completely avoided xml configuration. 5) Spring Boot allows to develop application using two approaches * Annotation Driven Configuration * Java Based Driven Configuration 6) In Spring Boot as programmer no need to create the container object explictly because SpringApplication.run(-) will return ConfigurableApplicationContext Object. 7) Spring Boot having support of embedded servers such as Tomcat,Jetty,undertow here we no need to arrange the servers explictly. 8) Spring Boot having support some of In-Memory Databases i.e.,H2. 9) Spring Boot gives Starters Concept to download dependencies required for project. 10) Spring Boot Exclusively support for MicroServices architecture application development. 11) Spring Boot gives built-in suppport of properties file i.e.,application.properties 12) Spring Boot having support of both properties & YAML File. Spring Boot Environmental Setup =============================== Step-1 : ======== * First Make sure we should install Java Software in our machine with minimium version will be Java8/Java11 Version NOTE ==== ** Always recommended to install Long Term Support(LTS) Version i.e.,Java8,Java11,Java17,Java21 ** Make sure We need to configure the path variable correctly for Java Software. path :::: c:\programFiles\Java\jdk8.10.12\bin ** We need to create new Variable under Environmental variables section i.e.,JAVA_HOME JAVA_HOME >>>>>>> c:\programFiles\Java\jdk8.10.12 Step-2 : ======== * We need to install at least one IDE for Developing the Spring Boot Application 1) STS IDE ========== >>>>> Navigate to https://spring.io/tools >>>>> Select "Spring Tools 4 for Eclipse" >>>>> Click on Following Button "4.18.1 - WINDOWS X86_64" >>>>> Automatically will download as JAR File. >>>>> After downloading the JAR simple extract into normal folder(Winzip/7zip) and find the STS executable file. >>>>> After opening executable file configure your workspace folder in our Hard drive to place all the Spring Boot Projects. >>>>> Create New Spring Boot Project by Navigating (Goto File Menu >>> Project Option >>>> SpringBoot >>> Spring Starter Project) >>>>> We need to provide the below details while creating spring boot project Service URL ::::: https://start.spring.io Name ::::::: 08_SpringBoot_FirstApplication Type ::::::: Maven Packaging ::::::JAR Java Version:::::17 Group ::::::: com.ashokit ArtifactId 08_SpringBoot_FirstApplication Version :::::: 0.0.1-SNAPSHOT Description ::::: Demo project for Spring Boot Package ::::::: com.ashokit (basepackage for component scan) >>>>> Select Spring Boot Version "3.4.4" >>>>> No Need to select "Any Starter" from List. >>>>> Click on Finish Button. >>>>> Finally Spring Boot Project gets Created in the STS IDE 2) Intellij IDE =============== >>>>> Navigate to https://www.jetbrains.com/idea/download/#section=windows >>>>> Download the executable file under "Community Edition" i.e.,Free & Open Source IDE >>>>> Complete the installation process similar to normal software installation process. >>>>> We can create sample Spring Boot Application from Spring Intializer Website and download the Spring Boot application as zip file and extract it in our local machine. >>>>> Open the intellijIDE and import the Spring Boot Project. Step-3: ======= * Create some of Spring Beans in Spring Boot application and Run the Main Class by right click on it and choose option "Run as" >>>> Again choose "Spring Boot Application/Java Application". Understanding Spring Boot Application Structure =============================================== SpringBootFirstApplication (Application Name) | | |---------------> src/main/java >>>>>>>>>>>> To Place source code of our application. | | | |-----> com | | | |----> ashokit | | | |------> SpringBootFirstApplication.java (Main Class) | |---------------> src/main/resources >>>>>>>>> To place configuration files for our application | | | |-----> application.properties >>>>>> Default configuration file in Spring Boot Application. | |---------------> src/test/java >>>>>>>>>>>>> To place your source code related to test cases. | | | |-----> com | | | |----> ashokit | | | |------> ApplicationTests.java (Test Main Class) |---------------> Maven Dependencies | | | |-----> *.jar Files | |---------------> pom.xml >>>>>>>>>>>>> Project Related Configuration File | | | | -----> Dependencies Configuration pom.xml ======= Spring-boot-dependencies(Parent Project) ^ | Spring-boot-starter-parent(child Project) ^ | Our Spring Boot Project(sub child Project) NOTE ==== 1. If programmer is not selected any starter during the spring boot project creation will get following starter by default i.e.,spring-boot-starter & spring-boot-starter-test will provides spring related functionalities. Spring Boot Internals ===================== Application.java (Main Class) ============================= package com.ashokit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } * Every Spring Boot Application execution will starts from Main method of Java Program * Inside the Main method we have below statement SpringApplication.run(Application.class, args); * run(-,-) is static method from SpringApplication Class and return type of the statement is "ConfigurableApplicationContext" Object i.e.,Spring Container Object. * ConfigurableApplicationContext(CI) is an child interface of ApplicationContext(I). * Inside run(-,-) lot of things doing by the Spring boot application. 1) StopWatch Got Started 2) Preparing Environment Object, If there is no active profile spring boot will fallback to "default" profile and this profile information will available in environment object. 3) Printing Banner in Console >>>> If Banner mode is off, It won't print the banner in the console If Banner mode is log, It will log the banner into log file If we don't set any banner mode, It will print the banner in console. 4) Creating ApplicationContext Container Object based on below starter respective Spring Container got Activated * Spring-boot-starter >>>> DefaultApplicationContextFactory >>>>>> AnnotationConfigApplicationContext * Spring-boot-starter-web >>> AnnotationConfigServletWebServerApplicationContext * Spring-boot-starter-webflux >>>> AnnotationConfigReactiveWebServerApplicationContext 5) Prepare and Refreshing the Context Object 6) Executing & Starting Listeners 7) Executing & Calling Runners. @SpringBootApplication ======================= * It is main annotation for every Spring Boot Application. * It is used to simplify the configuration and bootstrapping of Spring-based applications. * The annotation combines three commonly used annotations: @SpringBootConfiguration, @ComponentScan, @EnableAutoConfiguration. @SpringBootConfiguration ======================== * It is specialized form of the @Configuration annotation specific to SpringBoot Application. * Indicates that the class is a configuration class, providing bean definitions and other Spring configurations. It allows you to define beans and their dependencies explicitly. NOTE ==== @Configuration is an Parent Annotation @SpringBootConfiguration is an Child Annotation * In Spring boot Application we can represent configuration class with help of either any one of above two annotations. @ComponentScan ============== * Scans the specified packages and automatically detects and registers beans within those packages. * It enables component scanning and auto-detection of Spring components, such as controllers, services, repositories, and other beans. @EnableAutoConfiguration ========================= * Enables Spring Boot's auto-configuration mechanism. It automatically configures various Spring components and third-party libraries based on the classpath dependencies and the defined configurations. This reduces the manual configuration required to set up a Spring application. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++