#4 IoC and DI in Spring

πŸš€ Add to Chrome – It’s Free - YouTube Summarizer

Category: N/A

Building WordCloud ...

Summary

No summary available.

Transcript

00:00

welcome back aliens my name is z ready and let's continue the series on Spring framework now till this point we have talked about what is spring we have talked about the prerequisits and then in when we talk when we talked about spring framework we introduced a term there called dependency injection but

00:16

that was just a name what exactly this concept means in fact in this video we'll not just talk about dep injection but also ioc because spring started with this concept of course it has lot of features but then this Remains the code of it so what exactly these two terms

00:33

are so before we talk about it the target for this video is 200 comments okay so let's talk about ioc see when you talk about a typical application it can be a web application or the interace application see ultimately you want to give this application for your clients right so you have a client now this can

00:49

be a browser this can be your mobile application doesn't matter and then what you want on the screen is data so most of the application are data driven application you use it for the data right and typically this data is stored in a database now doesn't matter is it a

01:05

SQL database or no SQ database that's a different topic the point is the data is coming from database or maybe from some external service but then how do you send your data from database to client and that's where you write this server site code right now in Java we can do that with the help of serlet so somewhere in between you need a system

01:23

which will interact with the database right so when a client sends a request uh you get the response back and then uh your application interacts with the database right now this can be a standard application this can be a web application as well if it use web application you use saet in between so

01:39

this box here represents a selet or this is done with the help of selet but now we are talking about spring right and a typical application how it will look like we normally create multiple layers so of course we'll talk about this in detail when we talk about spring web but

01:56

typically we have multiple layers in between uh just to show you here so we got a layer here of course this will be a class in Java and then we have one more layer here of course this will be a class and we have one more layer here so

02:11

the role of this particular layer here is to uh accept the client request the role of this layer is to do any business logic and the role of this layer is to connect your database right so we have different classes for different work here right typically they are called as controllers services and uh repositories

02:30

right so don't go with the short form doesn't matter we have different layers that's important now uh the dependency becomes here is if you have a controller here who wants to talk to a service class basically in Java everything is object right so basically we have to

02:45

work with a concept of objectoriented programming and everything in Java is an object that means if you want to work with service in the controller we have to create object of a service inside the controller so what I'm saying is imagine there's a class called called controller

03:01

in which if you want to use service methods basically you need the object of service right so this is a class service and this is the reference so if you want to use the service inside the controller you have to basically

03:17

create the object of it and Java is object driven same applies to the uh service let's say if service wants to work with the repository imagine there's a class called service and in this class of course you need to

03:32

use some features of repository we have to create object of repository here and mind you this is not the object this is just a reference we have to literally create an object by using a new keyword that means in Java this is only three

03:47

classes right imagine if you have a Java application which has thousands of classes okay now that's exaggerated let's say you have hundreds of classes so you have to create a class you have to create object of one class in another class and not just for one we have multiple classes there so what if let's

04:04

say we have a philosophy here by saying hey you know let me make the work of java developer simple what if there's some external power who says Hey Java developer in your application you focus only on the logic let me handle the

04:20

object Creations that's a philosophy right so let someone else take care of it and as a d developer now you're happy is because you'll be saying what's difficult in object creation right we can simply use a new keyword and object can be created see it's not that simple when you say you are creating an object

04:36

you have to manage the entire cycle of it it's not just about creation it's also about managing the object destroying the object right and we don't do that right we simply create the object depending upon how many requests we get for every new request we create new object and sometime we don't need

04:52

multiple objects and still we do it we create those objects so what if you say hey let me focus on the logic let someone else in the world take care of it that's the concept of ioc which stands for inversion of control so what

05:09

it means is typically we create the object by ourself that means we have a control on the object creation but what if you give it to someone else that is inversion of control you're giving the control to someone else right that's ioc but then ioc is just a principle it's a

05:25

philosophy so we need certain technique to do it and that's where the concept of dependency injection comes here so dependency injection is the actual implementation of ioc so how do we Implement ioc in in spring or in Java is with the help of dependency injection

05:41

it's a it's a Concrete technique so a lot of people get confused between ioc and Di and they get conf they think they are similar yes they do the same thing almost the same thing but ioc is a principle right and design the depense injection is the design pattern it's the

05:56

actual thing which we do so in Spring to achieve ioc we use di depends the injection so what we are saying is we have some external thing uh in this case it is spring spring says you don't worry every time you want an object just ask for the object I will give it to you

06:12

that is injecting the object sounds cool right so that means if you talk about this particular example here where you have a controller and a service so let's say a controller here needs the object of service we don't have to actually say new service that's not our job here you

06:29

can simply say inject you can ask spring framework to inject the object you just mentioned the reference spring will give you the object that is called the dependency injection so basically there are three techniques to achieve dependency injection one is the

06:44

Constructor injection so what you do in the controller class is you create a Constructor and in that Constructor you pass the reference of service and you say now since I need service here inject the object that's one way uh the second way is Setter so maybe you can create a

07:00

set of methods for that service reference and uh you can do the set of injection the third way of doing is is the field injection so in Java we have a concept of loose coupling basically what you do is uh you don't have a tight you don't have a concrete implementation of

07:15

one class in the other uh you code for the interfaces right uh so if you use a field injection somewhere you are stopping it you're not able to mock test and all those stuff again it will make much more sense once we go forward uh so field direction is not recommended you can go for Constructor and Setter we are

07:32

going to use field injection in this course but we'll also focus on Constructor and Setter so that's depending the injection that simply means someone else is injecting the object in your application and that someone else in our case is a spring framework so I hope it makes some sense

07:48

and once we start implementing it it will make much more sense bye-bye