"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot Internals Date : 17/09/2024 (Session - 26) _____________________________________________________________________________________________________________________________ Today Session ============= 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. Spring-boot-jdbc-starter >>>>>>> JdbcTemplate,NamedParameterJdbcTemplate Spring-boot-web-starter >>>>>>> DispatcherServlet,ViewResolver -------------------------------------------------------------------------------------------------------- IIQ) If we remove the @SpringBootApplication annotations on the main class what will happens ? --> Spring Boot Application will run successfully but we couldn't get the features of Spring Boot such as Autoconfiguration,componentscan etc., --------------------------------------------------------------------------------------------------------- IIQ) @Configuration VS @SpringBootConfiguration Annotation ? -> 1) Both of these Annotations are functional one and same to mark the Java Class as Configuration classes. 2) @Configuration can be used only in Spring Applications but we can't use @SpringBootConfiguration in Spring Applications. @SpringBootConfiguration & @Configuration can be used in Spring Boot Applications. --------------------------------------------------------------------------------------------------------- Spring Boot Banner ================== * Banner is nothing but whenever spring boot application is started we can see Spring banner in the console. * As a programmer we can have control to disable the banner by using configuration and programmatical approach. * As a programmer we can have control to customize the banner of an Spring Boot Application. * We can have option to disable the banner of spring boot application we need to do add the below configuration application.properties ====================== spring.main.banner-mode=off * For the above entry we have three values i.e.,console(Default),log,off console >>>> Printing the Spring banner on the console. log >>>>> Printing the Spring banner in to log file. off >>>> Disabling the Spring banner for Spring Boot Application. * We can disable the banner of the Spring boot application through programatically with the help of below code SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); * We can customize the banner of the Spring boot application from "Spring" to "Ashok IT" with below URL to generate banner and download the banner file into our machine and place into src/main/resource folder of our project. https://springhow.com/spring-boot-banner-generator/ _ _ _ ___ _____ / \ ___| |__ ___ | | __ |_ _|_ _| / _ \ / __| '_ \ / _ \| |/ / | | | | / ___ \\__ \ | | | (_) | < | | | | /_/ \_\___/_| |_|\___/|_|\_\ |___| |_| spring.application.name=SpringBootRunnersApplicationDemo spring.banner.location=classpath:banner.txt >>> customized the location of spring boot banner spring.main.banner-mode=console NOTE ==== When we are moving the banner.txt file to required location at that time we need to update the maven project compulsory to reflect project structure.