#6 Dependency Injection using Spring Boot

Telusko
1 May 202413:34

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

00:00

πŸš€ 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.

05:01

πŸ” 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.

10:04

πŸ›  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

Dependency Injection is a design pattern used in software development to manage dependencies between classes. It involves the injection of dependent objects into a class, rather than the class creating the objects itself. In the context of the video, it is the core concept being demonstrated through the use of Spring Boot, where the Spring framework manages the creation and lifecycle of objects, thus reducing the coupling between classes.

πŸ’‘Spring Boot

Spring Boot is a popular Java-based framework used to create stand-alone, production-grade Spring applications with minimal configuration. In the video, the instructor uses Spring Boot to illustrate the implementation of dependency injection, showing how to set up a new project and configure it to utilize Spring's dependency management capabilities.

πŸ’‘Spring Initializer

The Spring Initializer is a tool provided by Spring that helps developers quickly set up a new Spring project with the necessary dependencies and configurations. In the script, the instructor mentions creating a simple project using the Spring Initializer, which is a foundational step in demonstrating dependency injection.

πŸ’‘IoC Container

IoC stands for Inversion of Control, and the IoC Container in Spring is responsible for managing the creation and lifecycle of objects in a Spring application. The container is central to the concept of dependency injection, as it is where all the objects are instantiated and dependencies are injected. The video script explains the role of the IoC Container in managing class objects and how it facilitates dependency injection.

πŸ’‘Java

Java is a high-level, class-based, object-oriented programming language that is widely used for developing applications. In the video, the instructor specifies Java as the programming language for the project, and the script includes Java code examples that demonstrate how dependency injection is implemented in a Java application using Spring Boot.

πŸ’‘@Component

The @Component annotation in Spring is used to declare a class as a Spring-managed bean, indicating that an instance of the class should be created and managed by the Spring IoC container. In the video, the instructor adds the @Component annotation to the 'Dev' class to enable Spring to automatically create its object, showcasing the automatic dependency injection feature of Spring.

πŸ’‘ApplicationContext

The ApplicationContext interface in Spring is a more full-featured version of the BeanFactory and is used to retrieve objects from the IoC container. In the script, the instructor discusses obtaining a reference to the ApplicationContext to manually retrieve bean instances, which is a direct way to interact with the IoC container for dependency injection.

πŸ’‘getBean

The getBean method is a function of the ApplicationContext in Spring that allows for the retrieval of an instance of a bean by its type or name. In the video, the instructor uses the getBean method to attempt to retrieve an instance of the 'Dev' class from the IoC container, demonstrating how dependency injection can be manually handled if needed.

πŸ’‘Main Method

The main method is the entry point of a Java application, marked by the signature 'public static void main(String[] args)'. In the script, the main method is used to run the Spring application, where the instructor discusses how Spring's dependency injection works in the context of the main method, including the instantiation of the IoC container.

πŸ’‘Annotation

In Java, annotations are a form of metadata that can be added to classes, methods, variables, etc. In Spring, annotations are extensively used to provide declarative configuration and behavior. In the video, the @Component annotation is an example of how annotations are used to mark a class for dependency injection by Spring.

πŸ’‘Bean

In the context of Spring, a bean is an object that is managed by the Spring IoC container. The container is responsible for the creation, configuration, and destruction of beans. The script mentions beans in the context of dependency injection, where the instructor explains that Spring creates beans for classes marked with the @Component annotation and manages their lifecycle.

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

play00:00

welcome back aliens my name is D ready

play00:02

and in this video let's try to implement

play00:04

dependency injection using spring boot

play00:06

now till this point we have talked about

play00:08

what is dependenc injection we have

play00:10

talked about what is spring Boot and

play00:12

then we have also created a simple

play00:14

project with the help of the spring

play00:15

initializer and we got the web project

play00:18

but we are not going to use this project

play00:19

basically we let's create a new project

play00:22

without the web and let's try to

play00:24

implement depense injection okay so what

play00:26

I'm going to do is first of all let's

play00:27

create the simple project for that I'm

play00:30

going to go to start. spring.io and here

play00:34

let's create a new project so I will

play00:35

click on Maven and I'm going to select

play00:37

Java here the version is

play00:40

3.2.5 group ID is the Isco project name

play00:44

is my app of course this can be anything

play00:47

but that's in my app uh the Java version

play00:49

I have is 21 so and the jar packaging

play00:52

I'm not going to add any dependency

play00:54

because when you say you want to go for

play00:56

web you have to add web dependency but

play00:58

if you don't add any dependency you will

play01:00

get a default springboard options I will

play01:02

click on generate and you can see we got

play01:04

the download and I've also unzipped it

play01:07

now it's time to open that in the IDE so

play01:10

we got this intellig idea Community

play01:11

version I will simply open that project

play01:14

so this is the project which I got just

play01:16

now and I will click on open uh in this

play01:19

window yeah okay so now you can see this

play01:22

is a default project nothing fancy

play01:24

there's no web part here uh if you go to

play01:26

the pom.xml we just got the spring boot

play01:30

stter nothing fancy a simple dependency

play01:33

right now what I want to do is let's

play01:36

experiment with dependency injection and

play01:38

we have talked about depend injection

play01:39

before uh basically when you have

play01:41

dependencies in one class normally what

play01:43

you do is you create the object of it

play01:45

now we want to inject it how we do that

play01:49

uh so let's create a main code so we

play01:52

already have a main file here so you can

play01:53

see we have a class and it says my app

play01:56

application weird name but that's fine

play01:58

for us and and then we are saying public

play02:01

void main this is your main code which

play02:02

takes the string arguments and here we

play02:05

are running a spring application so what

play02:08

it does is here so when you say Spring

play02:10

application. Run It basically creates a

play02:13

container okay now you'll be saying what

play02:14

is container here see what happens is

play02:17

when you talk about your uh project so

play02:19

let's say this is your project in this

play02:21

project you have lot of classes uh so

play02:23

let's say you have one class two class

play02:25

three class four class and let's see you

play02:27

have multiple classes Here and Now you

play02:30

are saying hey Spring framework it is

play02:31

your job to create object of this spring

play02:35

will say okay uh that's my job I will do

play02:38

it but question is where exactly spring

play02:40

will do it of course objects are created

play02:42

inside the jvm so you'll be having this

play02:44

big box here of jvm I mean this is your

play02:49

project uh this is jvm and this is where

play02:52

you create all the objects now when you

play02:54

talk about spring spring itself has its

play02:57

own container inside it so spring will

play02:59

have a container inside the jvm and they

play03:03

call this container as a ioc container

play03:07

so this particular section here this

play03:09

green box here is called the ioc

play03:11

container or you can say spring

play03:13

container that's fine now in this

play03:15

container you create all the objects so

play03:18

let's say from your project you don't

play03:19

want object of all the classes let's say

play03:22

you got object for this you got object

play03:23

for this so these two glasses you want

play03:25

the object and where exactly spring will

play03:27

create the object so spring is going to

play03:30

create the object inside this so this is

play03:33

where it will create the object so it

play03:35

depends upon how many objects you want

play03:37

and which classes object you want but

play03:39

this will create the object inside this

play03:41

container that means when you want to

play03:44

run this application the first thing you

play03:46

need is not the object but this

play03:48

container and this line here is

play03:51

responsible to uh create that container

play03:53

So when you say Spring application. Run

play03:55

it will simply run that container for

play03:57

you okay so we have the container ready

play03:59

right but then we want the object also

play04:01

and for the objects we want class so

play04:04

what I will do is I will create a very

play04:05

simple class here and normally I call my

play04:07

participants my students as aliens uh is

play04:11

just that I believe that we don't live

play04:13

in this real world we live in virtual

play04:14

world not physically but virtually

play04:17

because we create virtual Solutions

play04:18

right so we we live in virtual world and

play04:20

that's why I called alien and every time

play04:22

you see alien I'm writing imagine that's

play04:24

a developer or maybe I can also say Dev

play04:27

if that is not an issue so let's say Dev

play04:29

so let's say we have a Dev class here so

play04:31

in this world we all are objects right I

play04:33

know that's not a good thing to

play04:35

objectify people but let's say in this

play04:38

example every developer is an object so

play04:40

I'm my object you're the object and we

play04:42

came from the same class let's a Dev

play04:44

class and what is your job your job is

play04:46

to code so public void code and that's

play04:50

our job or maybe instead of saying code

play04:52

I will say build that sounds much better

play04:54

right uh see difference between coding

play04:56

and building is when you code you you

play04:59

write some straight ments but will it

play05:00

run there's no idea but when you say

play05:02

build you are actually building

play05:04

something and now here I'm going to

play05:06

print working on awesome project again

play05:10

just want to print it some print

play05:11

something and that's why I'm doing this

play05:13

and this is what I want to call so I

play05:14

want to call this method build now from

play05:16

where of course the execution starts

play05:18

from the main uh the question is how I'm

play05:20

going to call okay let me just put that

play05:22

in side by side so that you can see it

play05:24

so this is the method I want to call so

play05:26

this build method I want to call from

play05:28

here of course the way you can do that

play05:31

is you can go here and you can call

play05:33

build will this work of course not we

play05:35

know in Java if you want to call a

play05:38

method and if it is a non-static method

play05:41

we have we need object of it so that

play05:43

means to call build we have to create

play05:45

object of Dev so what you will do you

play05:47

will just come back here and you say Dev

play05:49

obj is equal to new Dev right and then

play05:53

using this obj you will be calling build

play05:56

and that's how thing works right so

play05:57

basically you call this and you run this

play05:58

example if I run if I click on run it

play06:01

will create the object of Dev and you

play06:03

you can see we got the statement which

play06:04

is working on the awesome project is it

play06:06

a good idea of course you it works and

play06:08

basically this is what we used to do

play06:10

when you don't use spring basically you

play06:12

create the object by yourself but the

play06:14

idea behind spring framework is you

play06:16

don't have to manage the objects spring

play06:18

will do it for you because when you say

play06:20

new Dev here what you're doing is you

play06:22

are manually creating an object inside

play06:26

the jvm but not in the container that

play06:28

means when you create this object it is

play06:30

your responsibility to manage the entire

play06:33

cycle of it we don't want to do that we

play06:34

don't want to create this object by

play06:35

ourselves we want spring to create it

play06:38

and how will you do that it's very

play06:40

simple now since this is a spring

play06:42

project I'm assuming that spring might

play06:45

be creating the object behind the scene

play06:47

I just have to use it right maybe the

play06:49

object is already there maybe this is

play06:52

the object is already there why you have

play06:53

to get a new object for Dev uh so in

play06:56

that case what I will do is I will not

play06:58

be creating this object so will not say

play07:00

new DA because that's what creat the

play07:02

object but if I remove it you know your

play07:05

compiler is giving you some bad words

play07:06

compiler says hey what you doing

play07:09

variable obj might not have been

play07:11

intialized okay so we have to iniz it

play07:14

and one way to do that is just to uh

play07:17

fake your ID your compiler you can send

play07:20

null and your your ID is now very happy

play07:23

at least you have assigned something but

play07:25

of course when you run this you will get

play07:26

one of the most famous era in the world

play07:29

uh and we love it no we just I'm just

play07:32

kidding and the other is the null

play07:34

pointer exception we don't want that

play07:36

right so how do we do this null

play07:39

assigning n is not a good idea so we can

play07:42

get this object from the uh container

play07:45

now question how do you talk to The

play07:46

Container because now you are in this

play07:48

main code container is there with the

play07:50

jvm how will you talk to The Container

play07:52

we want to get a hold on it how will you

play07:55

get the hold on it maybe we can get a

play07:56

reference of it right so if you can get

play07:59

a reference of the container you're good

play08:02

to go so basically the type of this

play08:05

container here so let's say the type of

play08:07

this ioc container is of type

play08:10

application context so the type of this

play08:13

particular object of course right this

play08:14

is object even the container inside your

play08:17

jvm which is your ioc container itself

play08:19

is an object right so for for that

play08:21

object there should be some type and the

play08:22

type is application context so what if

play08:25

you can simply get application context

play08:28

and you can see it came from this spring

play08:29

framework application context from

play08:31

Spring framework. context and we can use

play08:34

the reference of it I can say context

play08:36

equal to okay we have to create the

play08:39

object for this now this is weird right

play08:41

because ultimately we are saying we

play08:43

don't want to create the object but now

play08:45

it says we have to create the object see

play08:46

not exactly see application context will

play08:48

work only when you create the object of

play08:50

it right but what if I can get the

play08:52

object from Spring itself remember when

play08:54

we talked about this particular line at

play08:56

the start and I I told you that this

play08:58

particular line creates the container

play09:00

for you it does example if I click on

play09:02

this run here you can see this run

play09:05

method Returns the object so I just went

play09:08

to the source code of it basically I've

play09:10

decompiled the file with the help of IDE

play09:13

and this run method which we are calling

play09:16

of Spring application. Class it

play09:18

basically Returns the object of

play09:20

configurable application context if I go

play09:23

here it Returns the object oh sorry it

play09:25

extends the interface called application

play09:29

content text that means this run is

play09:33

returning you the object of application

play09:35

context that means we we already have

play09:37

the object so what you have to do is you

play09:39

just get this cut put it here and say

play09:43

equal to so what we are doing is we are

play09:44

ass signing this object which returns

play09:46

from run to the context once that is

play09:49

done I can simply use the context and

play09:52

say so context has multiple methods here

play09:55

and you tell me looking at this in the

play09:57

comments before you when I go forward I

play09:58

will take a pause

play09:59

which method you are going to use to get

play10:04

that

play10:05

object pause the video let me know in

play10:07

the comments okay so I I hope you have

play10:08

entered the answer so it's actually the

play10:11

get bean so in the get bean you have to

play10:13

mention which class object you want so I

play10:16

want the object of alien class that's it

play10:19

sorry not alien we are not going for

play10:20

alien anymore we are going for Dev okay

play10:22

so you can see we are saying that I want

play10:24

the object of Dev who has created the

play10:26

object it is spring that's what I'm

play10:27

assuming that the object is there in the

play10:29

the container I just have to use it and

play10:31

I'm doing the coding for that so now

play10:34

this will give me the object which is

play10:35

existing I'm assuming that it is there

play10:38

and I'm fetching it let's see if that

play10:40

works I will run this and if you see we

play10:43

got an error it says no qualifying Bean

play10:47

of type com. telescope. myapp dodev oh

play10:53

okay so we don't have this object in the

play10:54

container I was assuming that there's a

play10:56

there's a object but it's not there

play10:58

container is there for sure because this

play11:00

is what creates the container but inside

play11:02

the container the object is not there

play11:04

and the entire video got wasted is

play11:06

because spring says I'm not going to

play11:08

create the object why is not creating

play11:10

the object now if you remember in the

play11:11

one of the topic or one of the video we

play11:13

have mentioned that Spring by default

play11:15

will not create object of all the

play11:16

classes and we don't even want it

play11:18

because if spring creates object of all

play11:20

the classes and if you have 100 or

play11:22

thousand classes we don't want the jvm

play11:25

to be burdened with all these objects

play11:27

which we're not going to even use it and

play11:28

that's why spring says I'm not going to

play11:30

create the object by default you tell me

play11:32

which class objects you want and

play11:34

whatever you say I will create the

play11:36

object and the question is how will you

play11:37

talk to Spring framework maybe you need

play11:39

a config class or in the spring boot or

play11:43

we can use Java based configuration uh

play11:45

we can actually use something called an

play11:47

annotation so on top of your class

play11:49

whichever class object you want just say

play11:52

this class is a component just by

play11:54

mentioning this annotation here your

play11:57

spring understands that this is the

play11:59

class class which I have to manage so

play12:01

this is a managed Bean what it means is

play12:04

spring will create the object for you in

play12:06

the container so the moment you say

play12:07

component spring says now I know what's

play12:10

my job my job is to create the object I

play12:11

will do it for you and now let's see

play12:14

just by adding that component annotation

play12:16

is it working let's relaunch the code

play12:18

relaunch this application and it worked

play12:21

can you see that it says working on this

play12:23

awesome project so this is working right

play12:26

and that's how we get DEF injection so

play12:29

what we are doing is in this code we

play12:31

wanted the object of developer or Dev

play12:34

and spring is injecting that dependency

play12:37

okay now we can go bit more layers

play12:40

example let's say uh we got Dev and then

play12:43

Dev needs an object of a laptop of

play12:45

course as a programmer or as a developer

play12:46

you want to work on a laptop and you

play12:48

don't have a laptop here you're just

play12:50

saying working on a project but how in

play12:52

the air or maybe Oculus device or maybe

play12:54

Apple Vision Pro doesn't matter you need

play12:57

something to work on right and we don't

play12:58

have it so let's say in the next video

play13:01

let's try to create one more layer uh

play13:03

because in this we we got two layers

play13:05

right we got two classes so main needs

play13:07

object of Dev it is working but what if

play13:09

Dev needs object of laptop how that will

play13:12

work let's try to understand that in the

play13:14

next video but yeah I hope you got

play13:16

something from this video uh something

play13:18

about the deeny injection using spring

play13:20

boot where you are injecting a

play13:23

dependency in this particular section so

play13:26

that's it from this video uh I hope you

play13:28

remember the target the target for this

play13:29

video is uh 200 comments again and see

play13:32

you in the next video bye-bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Spring BootDependency InjectionJava TutorialIoC ContainerDevOpsCodingBuild ProcessSoftware DevelopmentSpring FrameworkBean Management