#6 Dependency Injection using Spring Boot
Summary
TLDRIn this instructional video, the host, D Ready, guides viewers through implementing dependency injection in a Spring Boot project. Starting with a basic Spring Boot setup without web dependencies, the tutorial progresses to explain the concept of dependency injection and the role of the Spring container, or IoC container. The host demonstrates creating a 'Dev' class, annotating it with @Component to signal Spring to manage its lifecycle, and retrieving it from the container using ApplicationContext's getBean method. The video concludes with a simple example of how Spring Boot can inject dependencies, setting the stage for further exploration in subsequent videos.
Takeaways
- 😀 The video discusses implementing dependency injection using Spring Boot.
- 🛠 The presenter guides through creating a new Spring project without web dependencies to focus on dependency injection.
- 📚 The script explains the concept of a container in Spring, which is an IoC (Inversion of Control) container managing object creation.
- 🌟 The video clarifies that Spring Boot does not automatically create objects for all classes by default to avoid unnecessary burden on the JVM.
- 🔍 It demonstrates how to use the `@SpringBootApplication` annotation to bootstrap a Spring application and create the container.
- 👾 The term 'aliens' is used by the presenter as a nickname for students or developers, emphasizing the virtual world of software development.
- 🛑 The script highlights the error that occurs when trying to use an object not managed by Spring, leading to a 'no qualifying bean' exception.
- 🔑 The solution to the error is using the `@Component` annotation to mark a class as a bean that Spring should manage and instantiate.
- 🔄 The video shows how to retrieve a bean from the Spring container using `getBean` method of the `ApplicationContext`.
- 🔗 The importance of configuring the Spring context properly to manage dependencies is emphasized, which is key to successful dependency injection.
- 🚀 The presenter teases the next video, which will explore more complex scenarios involving multiple layers of dependency injection.
Q & A
What is the main topic of the video?
-The main topic of the video is implementing dependency injection using Spring Boot.
What is the purpose of the Spring Initializer mentioned in the video?
-The Spring Initializer is used to create a new Spring Boot project with the necessary configurations.
Why is the speaker creating a new project without web dependency in the video?
-The speaker is creating a new project without web dependency to focus on implementing dependency injection without the complexity of web-related configurations.
What is the role of the '@SpringBootApplication' annotation in a Spring Boot project?
-The '@SpringBootApplication' annotation is a convenience annotation that adds '@Configuration', '@EnableAutoConfiguration', and '@ComponentScan' annotations to the class, which helps in creating the Spring container and enabling auto-configuration.
What is the term used to describe the container that Spring Boot creates for managing bean lifecycles?
-The term used to describe the container that Spring Boot creates for managing bean lifecycles is the 'IoC container' or 'Spring container'.
What does the speaker mean by 'dependency injection' in the context of this video?
-In the context of this video, 'dependency injection' refers to the process where Spring Boot automatically creates and manages the dependencies of a class, rather than the class creating its own dependencies.
How does the speaker initially try to call the 'build' method in the 'Dev' class?
-The speaker initially tries to call the 'build' method in the 'Dev' class by creating a new instance of the 'Dev' class using 'new Dev()' and then calling 'obj.build()'.
What is the problem with manually creating an object of 'Dev' in the 'main' method as shown in the video?
-The problem with manually creating an object of 'Dev' in the 'main' method is that it goes against the principle of dependency injection, where Spring should manage the creation and lifecycle of objects, not the developer.
Why does the speaker suggest getting the 'ApplicationContext' from the result of the 'SpringApplication.run()' method?
-The speaker suggests getting the 'ApplicationContext' from the result of the 'SpringApplication.run()' method because this method returns an object of 'ConfigurableApplicationContext', which can be used to retrieve beans managed by Spring.
What is the error encountered when trying to get the 'Dev' bean from the 'ApplicationContext' without declaring it as a component?
-The error encountered is 'No qualifying bean of type', which indicates that Spring does not have a bean of the specified type 'Dev' in the container because it has not been marked as a component to be managed by Spring.
What annotation does the speaker add to the 'Dev' class to make Spring Boot create and manage its instance?
-The speaker adds the '@Component' annotation to the 'Dev' class to make Spring Boot create and manage its instance.
Outlines
🚀 Introduction to Dependency Injection with Spring Boot
The video begins with an introduction to the concept of dependency injection using Spring Boot. The presenter, D ready, outlines the plan to create a new project without web dependencies and implement dependency injection. The process of creating a project through start.spring.io is detailed, including the selection of Maven, Java version, group ID, and project name. The presenter also explains the Spring Boot starter and the absence of additional dependencies to keep the project simple. The video sets the stage for exploring the Spring container, also known as the IoC container, which manages object creation and lifecycle.
🔍 Understanding the Spring Container and Dependency Injection
This paragraph delves deeper into the workings of the Spring container, explaining its role in managing class objects within an application. The presenter clarifies that the container is responsible for creating objects as specified by the developer, rather than the developer manually creating them. An example is given where a 'Dev' class with a 'build' method is created, and the presenter discusses the traditional approach of creating an object and calling a method versus the Spring framework's approach of letting the container manage object creation. The video also touches on the concept of a 'null' reference and the problems it can cause, leading into a discussion about how to obtain objects from the Spring container using the 'getBean' method.
🛠 Implementing Dependency Injection and Resolving Bean Errors
The presenter moves on to practical implementation, attempting to use the 'getBean' method to retrieve a 'Dev' object from the Spring container. However, an error occurs indicating that no qualifying bean of the specified type is found within the container. This leads to an explanation of Spring's default behavior, which is not to create beans for all classes but only for those explicitly marked as '@Component' or similar annotations. By adding the '@Component' annotation to the 'Dev' class, Spring is instructed to manage the 'Dev' bean, and upon relaunching the application, the dependency injection works as expected, with the 'Dev' object being injected and the 'build' method executed successfully. The video concludes with a teaser for the next topic, which will explore more complex dependency relationships, such as a 'Dev' needing a 'Laptop'.
Mindmap
Keywords
💡Dependency Injection
💡Spring Boot
💡Spring Initializer
💡IoC Container
💡Java
💡@Component
💡ApplicationContext
💡getBean
💡Main Method
💡Annotation
💡Bean
Highlights
Introduction to implementing dependency injection using Spring Boot.
Explanation of dependency injection and its importance in Spring Boot.
Creation of a simple Spring Boot project without web dependencies.
Understanding the Spring Boot initializer and project setup.
Concept of the Spring container and its role in managing objects.
Difference between creating objects manually and using Spring's container.
Creating a 'Dev' class to demonstrate dependency injection.
The process of calling a method from the main class without object instantiation.
The problem with manually creating objects and the Spring framework's solution.
Using the ApplicationContext to get a reference to the Spring container.
The error encountered when trying to fetch a bean that doesn't exist in the container.
Explanation of Spring's default behavior not to create objects for all classes.
The use of the '@Component' annotation to mark a class as a managed bean in Spring.
How adding the '@Component' annotation enables Spring to create the object.
Demonstration of dependency injection with the 'Dev' class and the 'build' method.
Introduction to the concept of a 'Laptop' class for a deeper layer of dependency injection.
Teaser for the next video to explore multi-layer dependency injection with 'Dev' and 'Laptop' classes.
Summary of the video's goal to understand and implement dependency injection in Spring Boot.
Transcripts
welcome back aliens my name is D ready
and in this video let's try to implement
dependency injection using spring boot
now till this point we have talked about
what is dependenc injection we have
talked about what is spring Boot and
then we have also created a simple
project with the help of the spring
initializer and we got the web project
but we are not going to use this project
basically we let's create a new project
without the web and let's try to
implement depense injection okay so what
I'm going to do is first of all let's
create the simple project for that I'm
going to go to start. spring.io and here
let's create a new project so I will
click on Maven and I'm going to select
Java here the version is
3.2.5 group ID is the Isco project name
is my app of course this can be anything
but that's in my app uh the Java version
I have is 21 so and the jar packaging
I'm not going to add any dependency
because when you say you want to go for
web you have to add web dependency but
if you don't add any dependency you will
get a default springboard options I will
click on generate and you can see we got
the download and I've also unzipped it
now it's time to open that in the IDE so
we got this intellig idea Community
version I will simply open that project
so this is the project which I got just
now and I will click on open uh in this
window yeah okay so now you can see this
is a default project nothing fancy
there's no web part here uh if you go to
the pom.xml we just got the spring boot
stter nothing fancy a simple dependency
right now what I want to do is let's
experiment with dependency injection and
we have talked about depend injection
before uh basically when you have
dependencies in one class normally what
you do is you create the object of it
now we want to inject it how we do that
uh so let's create a main code so we
already have a main file here so you can
see we have a class and it says my app
application weird name but that's fine
for us and and then we are saying public
void main this is your main code which
takes the string arguments and here we
are running a spring application so what
it does is here so when you say Spring
application. Run It basically creates a
container okay now you'll be saying what
is container here see what happens is
when you talk about your uh project so
let's say this is your project in this
project you have lot of classes uh so
let's say you have one class two class
three class four class and let's see you
have multiple classes Here and Now you
are saying hey Spring framework it is
your job to create object of this spring
will say okay uh that's my job I will do
it but question is where exactly spring
will do it of course objects are created
inside the jvm so you'll be having this
big box here of jvm I mean this is your
project uh this is jvm and this is where
you create all the objects now when you
talk about spring spring itself has its
own container inside it so spring will
have a container inside the jvm and they
call this container as a ioc container
so this particular section here this
green box here is called the ioc
container or you can say spring
container that's fine now in this
container you create all the objects so
let's say from your project you don't
want object of all the classes let's say
you got object for this you got object
for this so these two glasses you want
the object and where exactly spring will
create the object so spring is going to
create the object inside this so this is
where it will create the object so it
depends upon how many objects you want
and which classes object you want but
this will create the object inside this
container that means when you want to
run this application the first thing you
need is not the object but this
container and this line here is
responsible to uh create that container
So when you say Spring application. Run
it will simply run that container for
you okay so we have the container ready
right but then we want the object also
and for the objects we want class so
what I will do is I will create a very
simple class here and normally I call my
participants my students as aliens uh is
just that I believe that we don't live
in this real world we live in virtual
world not physically but virtually
because we create virtual Solutions
right so we we live in virtual world and
that's why I called alien and every time
you see alien I'm writing imagine that's
a developer or maybe I can also say Dev
if that is not an issue so let's say Dev
so let's say we have a Dev class here so
in this world we all are objects right I
know that's not a good thing to
objectify people but let's say in this
example every developer is an object so
I'm my object you're the object and we
came from the same class let's a Dev
class and what is your job your job is
to code so public void code and that's
our job or maybe instead of saying code
I will say build that sounds much better
right uh see difference between coding
and building is when you code you you
write some straight ments but will it
run there's no idea but when you say
build you are actually building
something and now here I'm going to
print working on awesome project again
just want to print it some print
something and that's why I'm doing this
and this is what I want to call so I
want to call this method build now from
where of course the execution starts
from the main uh the question is how I'm
going to call okay let me just put that
in side by side so that you can see it
so this is the method I want to call so
this build method I want to call from
here of course the way you can do that
is you can go here and you can call
build will this work of course not we
know in Java if you want to call a
method and if it is a non-static method
we have we need object of it so that
means to call build we have to create
object of Dev so what you will do you
will just come back here and you say Dev
obj is equal to new Dev right and then
using this obj you will be calling build
and that's how thing works right so
basically you call this and you run this
example if I run if I click on run it
will create the object of Dev and you
you can see we got the statement which
is working on the awesome project is it
a good idea of course you it works and
basically this is what we used to do
when you don't use spring basically you
create the object by yourself but the
idea behind spring framework is you
don't have to manage the objects spring
will do it for you because when you say
new Dev here what you're doing is you
are manually creating an object inside
the jvm but not in the container that
means when you create this object it is
your responsibility to manage the entire
cycle of it we don't want to do that we
don't want to create this object by
ourselves we want spring to create it
and how will you do that it's very
simple now since this is a spring
project I'm assuming that spring might
be creating the object behind the scene
I just have to use it right maybe the
object is already there maybe this is
the object is already there why you have
to get a new object for Dev uh so in
that case what I will do is I will not
be creating this object so will not say
new DA because that's what creat the
object but if I remove it you know your
compiler is giving you some bad words
compiler says hey what you doing
variable obj might not have been
intialized okay so we have to iniz it
and one way to do that is just to uh
fake your ID your compiler you can send
null and your your ID is now very happy
at least you have assigned something but
of course when you run this you will get
one of the most famous era in the world
uh and we love it no we just I'm just
kidding and the other is the null
pointer exception we don't want that
right so how do we do this null
assigning n is not a good idea so we can
get this object from the uh container
now question how do you talk to The
Container because now you are in this
main code container is there with the
jvm how will you talk to The Container
we want to get a hold on it how will you
get the hold on it maybe we can get a
reference of it right so if you can get
a reference of the container you're good
to go so basically the type of this
container here so let's say the type of
this ioc container is of type
application context so the type of this
particular object of course right this
is object even the container inside your
jvm which is your ioc container itself
is an object right so for for that
object there should be some type and the
type is application context so what if
you can simply get application context
and you can see it came from this spring
framework application context from
Spring framework. context and we can use
the reference of it I can say context
equal to okay we have to create the
object for this now this is weird right
because ultimately we are saying we
don't want to create the object but now
it says we have to create the object see
not exactly see application context will
work only when you create the object of
it right but what if I can get the
object from Spring itself remember when
we talked about this particular line at
the start and I I told you that this
particular line creates the container
for you it does example if I click on
this run here you can see this run
method Returns the object so I just went
to the source code of it basically I've
decompiled the file with the help of IDE
and this run method which we are calling
of Spring application. Class it
basically Returns the object of
configurable application context if I go
here it Returns the object oh sorry it
extends the interface called application
content text that means this run is
returning you the object of application
context that means we we already have
the object so what you have to do is you
just get this cut put it here and say
equal to so what we are doing is we are
ass signing this object which returns
from run to the context once that is
done I can simply use the context and
say so context has multiple methods here
and you tell me looking at this in the
comments before you when I go forward I
will take a pause
which method you are going to use to get
that
object pause the video let me know in
the comments okay so I I hope you have
entered the answer so it's actually the
get bean so in the get bean you have to
mention which class object you want so I
want the object of alien class that's it
sorry not alien we are not going for
alien anymore we are going for Dev okay
so you can see we are saying that I want
the object of Dev who has created the
object it is spring that's what I'm
assuming that the object is there in the
the container I just have to use it and
I'm doing the coding for that so now
this will give me the object which is
existing I'm assuming that it is there
and I'm fetching it let's see if that
works I will run this and if you see we
got an error it says no qualifying Bean
of type com. telescope. myapp dodev oh
okay so we don't have this object in the
container I was assuming that there's a
there's a object but it's not there
container is there for sure because this
is what creates the container but inside
the container the object is not there
and the entire video got wasted is
because spring says I'm not going to
create the object why is not creating
the object now if you remember in the
one of the topic or one of the video we
have mentioned that Spring by default
will not create object of all the
classes and we don't even want it
because if spring creates object of all
the classes and if you have 100 or
thousand classes we don't want the jvm
to be burdened with all these objects
which we're not going to even use it and
that's why spring says I'm not going to
create the object by default you tell me
which class objects you want and
whatever you say I will create the
object and the question is how will you
talk to Spring framework maybe you need
a config class or in the spring boot or
we can use Java based configuration uh
we can actually use something called an
annotation so on top of your class
whichever class object you want just say
this class is a component just by
mentioning this annotation here your
spring understands that this is the
class class which I have to manage so
this is a managed Bean what it means is
spring will create the object for you in
the container so the moment you say
component spring says now I know what's
my job my job is to create the object I
will do it for you and now let's see
just by adding that component annotation
is it working let's relaunch the code
relaunch this application and it worked
can you see that it says working on this
awesome project so this is working right
and that's how we get DEF injection so
what we are doing is in this code we
wanted the object of developer or Dev
and spring is injecting that dependency
okay now we can go bit more layers
example let's say uh we got Dev and then
Dev needs an object of a laptop of
course as a programmer or as a developer
you want to work on a laptop and you
don't have a laptop here you're just
saying working on a project but how in
the air or maybe Oculus device or maybe
Apple Vision Pro doesn't matter you need
something to work on right and we don't
have it so let's say in the next video
let's try to create one more layer uh
because in this we we got two layers
right we got two classes so main needs
object of Dev it is working but what if
Dev needs object of laptop how that will
work let's try to understand that in the
next video but yeah I hope you got
something from this video uh something
about the deeny injection using spring
boot where you are injecting a
dependency in this particular section so
that's it from this video uh I hope you
remember the target the target for this
video is uh 200 comments again and see
you in the next video bye-bye
5.0 / 5 (0 votes)