Spring Boot is a framework for building applications in the Java Programming language.
Spring Boot
makes it easy to create stand-alone, production-grade spring based applications that you can just run.
The core Spring framework already reduces boilerplate code and provides a lot of helpful features for
Java applications. However, Spring Boot takes this convenience to the next level by focusing specifically
on reducing the effort required to set up and configure a spring application.
While Spring does simplify many tasks like creating Web applications, working with databases, managing
transactions and more, But setting up a spring project can still involve quite a bit of manual
configuration. This is where Spring boot steps in:
1. Does Auto configuration
2. Create Standalone applications – no need of any external server to deploy spring boot jar because
Spring Boot already has an embedded TomCat Server, which is self contained and independent of
external environment.
Without Spring Boot, setting up a basic Restful API requires manual configuration. See in above picture.
We need to manually setup the spring application context in the main method.
We use the @SpringBootApplication annotation on the main class. This single annotation replaces the
need for setting up a manual spring application context. We no longer need to explicitly create an
application context using AnnotationConfigApplicationContext as Spring Boot handles that behind the
scenes.
We use SpringApplication.run() to start the application, and spring boot takes care of configuring the
embedded Web Server and other necessary components.
The @SpringBootApplication annotation alone brings in a lot of pre-configured features including
automatic component scanning and embedded server configuration, which would have required more
steps in traditional spring setup.
Get Familiar with Spring
Beans: In Java Spring Boot, a bean is an object that is instantiated, assembled, and managed by the
Spring IoC (Inversion of Control) container. These objects form the backbone of your application and are
defined using configuration metadata, which can be provided through XML, annotations, or Java-based
configuration. Once any bean (object) is created in the code, and the whole code base will use the same
created object, it won’t reinitialize it again.
When a class is marked as a bean, the Spring container handles its lifecycle, including instantiation,
dependency injection, and destruction. Beans can be defined using annotations like @Component,
@Service, @Repository, or @Controller, or through XML configuration files.
JAR: Java Archive, it can be run simply using command no deployment.
WAR: Web Application Archive, this needs to be deployed on external web server.
Build Tool
Maven: It is a build automation tool, it simplifies build, and manage dependencies. It is for defining
how .java files get compiled to .class and then packaged into .jar/.war/.ear files.
Dependency-Injection: It means outsourcing the task of object creation.
The IoC Container is responsible to instantiate, configure and assemble the objects. The IoC container
gets informations from the configuration and works accordingly. We can configure IoC container in
following ways:
1) XML
2) Annotations
3) Java Configuration class
There are three ways of dependency injection into Bean:
1) Constructor
2) Setters
3) Property
Type of Beans:
Note: Last three are used in web application.
Here we have used getBean method to pick bean from config.xml.
Now we have created two beans referring same memory location.
if the result is true then it is Singleton Bean, and if it gives false then Prototype. We can also use @Scope
annotation on vehicle class. In that case for making it prototype we need to write @Scope(“prototype”)
on top of the class.
This is Singleton
and below one is Prototype
Here we just need to add scope element in the bean and default it to prototype.
Now in SpringBoot to create a bean we just need to add @component annotation on top of the class,
instead of creating bean in config.xml.
Might be wondering, why are we picking xml here again. So, now context: component scan tag will
search and scan for components in the given base package.
and now we just need to call and run
Note: Spring supports default bean ids i.e. even if we don’t pass id in @component annotation it will
work. It takes by default name as id by keeping first letter as small case.
Dependency – Injection using Annotation
1. Constructor Injection
@Autowired: Annotation used for Dependency injection. Whenever we add @Autowired on top of
constructor, spring will scan for the classes/Interfaces with @component on top of them being passed in
the constructor argument. Now it will start looking for its implementation throughout package.
2. Setter Injection: same as above
3. Field Injection: no need of any constructor/setter just add the annotation on top of the field.
What if there are multiple implementations of the interface for which @Autowired is looking for. Then it
will get confused and then @Qualifier annotation comes into picture.
Lets suppose in above example there is one more class implementing DiscountService i.e.
NoDiscountServiceimpl. Then we need to keep @Qualifier with @Autowired and provide beanID.
We can inject application.properties into our app using @Value annotation. For this just like how we
need to tell config that for components which base package to scan, here also we need to tell to load
from the resource location. See below
In starting we have learnt that we can configure IOC container in three ways:1. XML, 2. Annotations and
3. Java Configuration Class.
We have seen that for XML and Annotations method uses Config.xml , but can we totally remove this
xml. The answer is yes using Java Configuration class
Use @Bean annotation to create bean in java config class by creating a method and adding this
annotation on top of it and @PropertySource for referring application.properties file.
Spring MVC Architecture
It is a framework for building web applications in java which also uses features of spring like DI and IOC.
It is based on Model View Controller Design Pattern.
@Controller annotation indicates that the annotated class is a web controller. This annotation serves as
a specialization of @Component. Controller handles incoming HTTP requests and determines the
appropriate response to send back.
@RequestMapping annotation is used for mapping web requests/end-points onto methods in request
handling classes.
@RequestParam annotation is used to bind the values of a query string to a controller method in the
Spring MVC framework.