#11 Autowire 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 readyy and let's continue this series on Spring framework now to this point we were able to achieve Constructor and seter injection it's time to talk about Auto wiring if you remember when we talked about spring boot we have done this in

00:15

Spring boot we have done with auto wiring but here also without spring boot let's try to explore it and even before we do that if you remember the spring boot videos we have talked about different classes and interfaces because in this case we only have laptop and Dev

00:32

developer uh what we don't have is computer interface and desktop implementation of it so let's create those and let's get forward so what I will do is I will just go back here and say let me just copy and paste or maybe use the same a laptop class but this

00:47

time we'll name it as desktop and in desktop we'll change certain things I will just close this part and in desktop what I will do is when I say uh the structor should be desktop created and in the compiling I

01:03

will say compiling with or compiling in desktop okay so these are the changes I made so in the laptop it says compiling I will just change it to compiling in laptop okay uh nothing fancy just two different classes for the same concept

01:19

and when I say concept these two are are the same type right so they both are computer so what I will do is I will just extract the interface out of it extract the interface and and we'll name this as computer uh you can do this in different IDs otherwise you can just

01:34

simply create an interface and have this method which we want so if you go to computer okay vo compile so I wanted this method I don't know what I made some mistake while refactoring it doesn't matter so you can see we got the interface which is computer and both

01:49

these classes the laptop and desktop need to implement computer so I will say implements computer if this is not happening by default in your ID just try to manually type it that should not be a problem so we got the interface computer and then we got two implementation

02:05

laptop and desktop okay just to go with this concept of coding for the interfaces now since we have made those changes I also want to change my developer now developer should not be dependent on a laptop right so developer should be dependent on a computer so I will just make mention computer here and

02:22

here also I will just say this is a type I would say comp even comp works okay and then since we have made those changes I have to also change the in fact I will just remove this particular Constructor of parameters construct of Dev and then we have to make two changes

02:39

here so when I said two changes basically I'm I want to change the G sets not for laptop this time but for the computer so I will just say generate G sets for the computer I will say okay and now we got this two G set I me we got these two methods and then uh here

02:57

also in the build I will say com. compile not laptop do compile because as a developer it doesn't matter which one you want to use which one you're using basically desktop or laptop it should work right uh so we made few changes and will this work that's a question let's

03:12

try I have not made any changes in the uh spring.xml file so there's no confusion change let's see if this works and let's see what breaks when we do that so yeah we got the error and if you see the error it says error creating a bean DEA of course but why uh it says

03:28

cannot resolve to a Constructor yeah because we have removed the Constructor and I think in the XML file we are still using this Constructor so what I will do is I will just remove everything since we know now how to we use it so let me remove everything okay so Dev is

03:43

basically empty now and we got a laptop and the name for the ID for the laptop is Lab One what I want to do is I want to set some properties right so we have to basically set the property for uh the two things if if you go to Dev I'll just close this we got two properties we got comp and we got age and maybe we don't

04:00

even need age now let's only focus on comp okay U of course even if it is there it should not make any difference but I just want to keep the code a bit clean so let's say we have only one dependency which is of computer here and if I go back to the XML file now the dev

04:16

needs to have a computer object the property so I will say property the name of the property is comp but what should be the value of course you cannot set a Valu is because this is not a primitive type this is a reference type so inste of value it should be ref the question

04:32

is what it is referring to now computer is an interface right so if you look at developer it is dependent on a computer and computer itself is interface so I can't simply create an object of the interface right so I have to look for the implementation and in this case we got two implementation we got desktop

04:48

and we got laptop but if you look at the bean creation we are not creating the object for desktop we are just creating the object for laptop so basically in your container when you run this if it is it works basically you will get two objects one for your developer and one

05:06

for your laptop okay so we will get these two objects now laptop itself is a computer right so can we just inject laptop in this developer when it is asking for uh the comp yes we can so what we can simply do is we already have the object created for laptop which is

05:22

lab one we can simply refer it here our job is done right so now if I run this I hope this time it will work and if not let's debug it but it works you can see it says uh working on the awesome project laptop Constructor developer Constructor no where it's sayings desktop construct because we have not

05:39

geted the bean here and then it says compiling in laptop so perfectly makes sense but how about I want the object for the desktop as well so I can say Bean ID and I can say this is desk one and I say class the class is com. telescope.

uh desktop and then in here

05:56

okay we don't have any property there so now we got two objects and now if I on this look at the output basically you will also get the desktop Constructor so it depends upon how many beans you mention here okay so now you even got the desktop object here okay so in total

06:11

we got three objects but in developer Which object we are using we are using the lap one again we have done this before we know how it is referring now but yeah if you want to say this is desk one that's your choice I mean you can mention what property you want do you want a desktop or do you want a laptop so you can change that in XML file and

06:27

now it says compiling in desktop so the way you change that it will basically inject that particular object in the developer so it will inject this not this one okay things are working out but what if I change the name here so let's say uh the ID for laptop is comp okay

06:46

and will this work now of course I have to change here as well it will work because it will not be using com it will be using desk one but what if I say comp will there be an issue if you have the same name uh of course not see this comp is basically referring to the developer

07:02

variable name which is comp and the second comp this one is referring to the name of the Bean or the IDE of The Bean which is laptop Bean which is comp and now if you done this of course it will refer to the laptop and you can see it says compiling a laptop so our theory is working but don't you think these two names are same when you say name is comp

07:20

uh the reference also comp what if I just skip this part I mean I spring is so awesome right uh if spring is that awesome I want spring to actually connect that automatically what we are doing is we are doing wiring and I want this wiring to be happening

07:36

automatically we have done that in spring board here also if I just comment it and if I run uh let's see if spring is happy about it no no spring is not happy spring says it's still null null point of exception it's because we are commenting it and we are assuming that it will do auto wiring but spring says

07:53

no no no I will not automatically do it you have to tell me to do it so what you can do is you can just in the tag you can add one more attribute called autowire if you can see autowire and you can say by name now what it will do is it will check okay so in this particular

08:09

developer class we got only one property of comp the computer property the name is comp what I will do is I will search for this name in this particular container so in this container do we have any object with com yes so we have this name here so for this object we

08:25

have com for this object it is desk one so it was search hey do we have any object with name Comm and yes we do have it uh which is here so it will try to connect it and that's Auto wiring so if I run this your spring is Happy it says

08:40

compiling in laptop he happy so basically that's how you use autowire but the problem is you can't use desktop here because you can't have two things with the same name example if you if you say both are comp uh there will be a confusion and you can see a huge

08:57

confusion it says being com is already used right so we cannot use the same ID two times and that's why they IDs right it should be unique uh so we can have it comp and comp one but let's say uh if you want to use desktop as well one

09:12

thing you can do you can remove the IDS or uh you can change by saying hey I don't want to go by name I want to go by type even you can do that so this time I'm not saying by name I'm saying by type so when you're searching for comp is basically searching for these two

09:27

components here and says okay we got laptop and we got desktop both are computer what I will do time bu is I will just commend this laptop and let's see if that works so by type it is searching for the type of a computer and desktop is a type of computer but the

09:43

problem starts when you have both now if you see we got two beans of course they have different name but doesn't matter we are not even searching by name we sear searching by type the type of computer and desktop and laptop both are computers and now if you this spring is

10:00

not happy spring says hey you know I was searching for one but I found two there are two beans which are claiming that they are computers I can't I can't select one I don't want to be biased as a programmer we can be biased not the framework so what we can say okay in

10:17

case of confusion give me the laptop so I will say primary is equal to true so you can use the primary attribute so primary says in terms of confusion go with this particular Bean which is the laptop so we are saying primary is true and we have done this in the spring boot

10:34

as well with the help of at primary annotation but here I'm using the attribute and it says uh compiling with laptop uh so now I hope you are able to connect what we have done in the spring Boot and what is happening here of course XML is not something we love

10:49

about but I just wanted to explain you what is happening behind the scene so now uh this laptop object is becomes the primary one we can do actually one more thing which which not talked about which is the app. Java in this if you see we are saying get bean and when you say get

11:05

bean we are asking for the ID right I don't want to mention the ID what if I mention the type of the object I want I want the object of or not computer I want the object of Dev now will this work so we are not searching by name

11:20

here we're searching by type and when you to search by type you don't even have to type cust it so it reduces the number of words you write and if you run this it works right so you have a choice you can you can get bean by name or you can get bean by type so in this case uh

11:38

if you see the XML file the name is not important the type is important so you're searching for the object by type so that that's how basically you work with the autowire concept so that's it from this video where we talked about the auto wiring and I hope uh the spring

11:53

framework without spring boo makes sense now uh enough of spring framework now let let's focus on Spring boot more and then we'll move towards the project so I hope you enjoying the series where we are talking about spring and spring boot uh and it's time to focus more on spring

12:09

board bye-bye