0% found this document useful (0 votes)
10 views7 pages

Spring Boot Overview and Application Guide

Spring boot

Uploaded by

ajayhumendra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Spring Boot Overview and Application Guide

Spring boot

Uploaded by

ajayhumendra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

===========

Spring Boot
===========

=> Spring Boot is one approach to develop Spring Based Applications with less
configurations.

Spring Boot = Spring Framework - Xml Configurations

=> Spring Boot is not replacement for Spring Framework. Boot developed on top of
Spring Framework

Note: All Spring Framework concepts can be used in Spring Boot also.

=======================
Spring Boot - Advantages
=======================

1) Less Configuration & No Xmls Configurations

2) Pom Starters to simplify dependencies configuration

Ex: web-starter, jpa-starter, security-starter, mail-starter

3) Auto Configuration

4) Embedded Servers (Ex: Tomcat, Jetty, Netty)

5) Actuators (Production Ready Features)

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

Spring Boot 1.0 released in 2014

Current version of Spring Boot is 3.x ===> Nov-2022

Note: Java 17 is mandatory to work with Spring Boot 3.x version

=============================
Spring Boot Application Creation
=============================

=> We can create boot application in 2 ways

1) Initializr website ([Link])

2) Spring Starter Project in IDE

Note: If we try to create boot application using IDE then internally IDE will
communicate with Intializr website to create the project.

Note: Internet Connection is mandatory for system to create Spring Boot


Application.

=============================================
Options to choose While creating boot application
=============================================

Build Tool : Maven / Gradle

Language : Java / Groovy / Kotlin

groupId : It represents company name / Project Name

Ex: [Link]

artifactId : It represents project name / module name

Ex: MyFirstApp / UserManagementApp / TicketBookingApp

version : 0.0.1 - SNAPSHOT

SNAPSHOT - Project under development

RELEASE / FINAL - Project development completed

packageName : It represents base package in the project

Ex: [Link]

==================================
Spring Boot Application Folder Structure
==================================

src/main/java : To keep our project source code

- [Link] : It is start class of the spring boot (main


class)

src/main/resources : To keep project configuration files

- [Link] / yml

src/test/java : To keep junit code (unit testing)

- [Link]

src/test/resources : Unit testing related config files goes here

Maven dependencies : Downloaded jars will be available here

[Link] : Maven configuration file (dependencies)

=======================
Spring Boot Start Class
=======================

@SpringBootApplication
public class Application {

public static void main(String[] args) {


[Link]([Link], args);
}
}

=> It is entrypoint for boot application execution


=> @SpringBootApplication annotation is equal to below 3 annotations

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

=> Spring Boot start class will act as Configuration class because of
@SpringBootConfiguration annotation

=> In Spring boot application auto configuration feature will be available


because of @EnableAutoConfiguration annotation

=> Component Scan will be performed in spring boot because of @ComponentScan


annotation

Note: Package Naming convention will play important role in component scanning

basePackage : [Link]

[Link] -- will be scanned


[Link] -- will be scanned
[Link] ---- will not get
scanned

==================================
What is [Link] ( ) method
==================================

=> Spring Boot start class main ( ) method will call [Link] ( )
method.

=> [Link] ( ) is entry point for boot application execution. This


run ( ) method will return reference of IOC.

Note: SpringApplication is a predefined class and it will identify what type of


application we have created based on dependencies added in [Link] file.

=> If we add "spring-boot-starter" ===> standalone app


=> for standalone app, It will use "AnnotationConfigApplicationContext"
class to start IoC container

=> If we add "spring-boot-starter-web" ===> Web application


=> For web apps, "AnnotationConfigServletWebServerApplicationContext"
class will used to start IoC container

=> If we add "spring-boot-starter-webflux" ===> Reactive Application


=> For reactive app, "AnnotationConfigReactiveWebServerApplicationContext"
class will be used to start IoC container

Note: Based on Type of our application, It will start IOC container.

=> run ( ) method will print banner on the console (i.e spring logo).

=> run ( ) method will start IoC container

=> run ( ) method will call runners

=> run ( ) method will return context of IoC container


######## ConfigurableApplicationContext context =
[Link]([Link], args); #######

============================
What is Banner in Spring Boot ?
============================

=> When we run boot application, it will print spring logo on the console that
is called as Banner in Spring Boot.

=> Spring Boot banner is having 3 modes

1) Off
2) Log
3) Console ( default )

=> We can set banner mode using below property in "[Link]" file

[Link]-mode=off

=> We can customize banner text in spring boot application by creating


"[Link]" file in src/main/resources folder

URL To Generate ASCII Text :


[Link]

====================
Runners in Spring Boot
====================

=> Runners are used to execute the logic only once when boot application got
started

=> [Link] ( ) method will call runners

=> In Spring Boot we have 2 types Runners

1) ApplicationRunner ( I )

2) CommandLineRunner ( I )

@Component
public class MyAppRunner implements ApplicationRunner {

@Override
public void run(ApplicationArguments args) throws Exception {
[Link]("AppRunner :: run ( ) method executed.....");
}
}

@Component
public class MyCmdRunner implements CommandLineRunner{

@Override
public void run(String... args) throws Exception {
[Link]("MyCmdRunner :: run ( ) method called...");
}
}

================
Runners Usecases
================

1) Loading data from db to cache memory

======================================
[Link] file & [Link]
======================================

=> [Link] & [Link] files are used to manage


configuration properties in the application

Ex: data source properties, smtp properties & application messages


etc...

=> properties file is used to configure properties in linear structure

Ex:

[Link]=ashokit
[Link]=ashokit@123
[Link]=jdbc://mysql:localhost:3306/db
[Link]-class-name=[Link]

=> YML / YAML file is used to configure properties in hierarichal structure

Ex:

spring:
datasource:
url: null
username: null
password: null
driver-class-name: null
main:
banner-mode: false
application:
name: app

=> Properties files are used only in java language

=> YML files used by several programming languages

Note: Indent spaces are very important in YML files.

=> In Properties, for every profile seperate properties file should be created.

application_dev.properties
application_qa.properties
application_prod.properties

=> In YML, multiple profiles can be configured in single YML file ( three ---
will be used to seperate profile)
Note: Spring Boot supports both .properties and .yml.

Note: Recommended to use .yml files

======================
Spring Boot - Summary
======================

1) What is Spring Boot

2) Spring Boot Version History

3) Advantages of Spring Boot

4) Spring Boot Application Creation

5) Spring Boot Application Folder Structure

6) What is Start Class

7) What is @SpringBootApplication

8) What is Component Scanning

9) What is base package naming convention ?

10) What is [Link] ( ) method in Spring Boot ?

11) How IoC container will be created in Spring Boot ?

12) Which class will be used to start IoC in spring boot ?

13) What is the return type of [Link] ( ) method ?

14) What is Banner in Spring Boot ? Can we customize that ?

15) What is Runner in Spring Boot ?

16) What is [Link] file in Spring Boot ?

17) What is Dependency Injection ?

18) What type of DI we can perform in Spring Boot ?

19) What is Bean Scope ?

20) What is Autowiring ?

21) What are the modes available for Autowiring ?

22) What is Bean Lifecycle ?

23) How to represent java class as Spring Bean ?

24) What is the difference between @Component & @Bean annotations ?

25) Which dependency injection is better (SI or CI or FI ) ?

26) @Comonent vs @Service vs @Repository annotations ?

27) What is @Value annotation ?


28) [Link] vs [Link]

29) What is @Primary annotation ?

Common questions

Powered by AI

The main difference between application.properties and application.yml files lies in their structure and syntax. The application.properties file uses a linear structure suitable for simple key-value pairs, while application.yml uses a hierarchical structure ideal for representing complex property relationships. YML files are more versatile, allowing multiple profiles to be configured in a single file using the '---' separator. In contrast, properties files require separate files for each profile. YML files are generally recommended due to their readability and support for complex data structures .

The SpringApplication.run() method serves as the entry point for launching a Spring Boot application. It starts the application context (IoC container) which is determined by the dependencies present in the pom.xml file. For standalone applications, it uses the AnnotationConfigApplicationContext class, for web applications, the AnnotationConfigServletWebServerApplicationContext class, and for reactive applications, the AnnotationConfigReactiveWebServerApplicationContext class. It also manages the lifecycle of the application, prints the banner, and executes any runners defined .

Java 17 is mandatory for Spring Boot 3.x applications because it ensures compatibility with the latest features and performance improvements offered by the recent Java LTS (Long-Term Support) version, which provides benefits in terms of stability, security, and language enhancements that are crucial for modern applications .

A Spring Boot application can be created using either the Spring Initializr website (start.spring.io) or through a Spring Starter Project in an IDE. The choice of build tool can be Maven or Gradle, and the language used can be Java, Groovy, or Kotlin. The creation process involves specifying the groupId (representing the company name or project name), artifactId (the project or module name), and version, among other configurations. An internet connection is necessary as the IDE communicates with Initializr to set up the project .

Component scanning in a Spring Boot application is essential because it automatically detects and registers beans within the application context, avoiding manual configuration. It relies on the @ComponentScan annotation, which scans the base package and its sub-packages for classes annotated with @Component, @Service, @Repository, or @Controller, ensuring they are registered as beans. The base package naming convention is crucial as incorrect package naming can lead to components not being included in the scanning process, potentially causing runtime errors .

Spring Boot enhances the development of production-grade applications by providing a streamlined setup with features like embedded servers, which eliminate the need for a separate application server, reducing deployment complexity. It offers production-ready capabilities via Actuators that provide monitoring and management endpoints, simplifying application health checks and diagnostics. Auto-configuration and starter dependencies further reduce setup time, and the focus on executable JAR files allows applications to run seamlessly in various environments. Together, these features support robust and easily deployable applications suitable for production use .

By default, a Spring Boot application's banner displays an ASCII art representation of the Spring logo on the console. This banner can be customized by creating a "banner.txt" file within the src/main/resources directory and modifying its content to display custom text or art. The banner mode can also be altered using the application.properties file with options to turn it off, print it to logs, or use the console. Custom ASCII art can be generated using online tools such as those found at patorjk.com .

The @SpringBootApplication annotation combines three crucial annotations: @SpringBootConfiguration for specifying configuration properties, @EnableAutoConfiguration for enabling auto-configuration to automatically set up beans for application needs, and @ComponentScan for scanning components within the application's package, which streamlines the setup process by reducing the need for manual configuration .

Runners in Spring Boot execute logic immediately after the application has started. They are implemented using ApplicationRunner or CommandLineRunner interfaces. These components allow developers to run specific tasks like loading initial data or executing scripts once the Spring Boot application initialization is complete. Runners are defined by creating classes that implement these interfaces and overriding their run methods to specify the operations to be performed .

Spring Boot simplifies the configuration of Spring applications through features such as simplified dependency management using Pom Starters (e.g., web-starter, jpa-starter), auto-configuration which automatically configures beans that are likely to be needed, and the elimination of XML configuration files by using annotations. It also supports embedded servers like Tomcat, Jetty, and Netty, allowing applications to run standalone without needing external application servers .

You might also like