#9 Spring XML Config

πŸš€ 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 a ready and let's continue the series on Spring framework now we are basically trying to create a spring project without spring Boot and of course for that we have to do lot of configuration and to achieve that we have basically created the project and in that we do have a Dev

00:16

class here in which you have a method called build and what we are trying to do is we are trying to call the build but for that we we need object of Dev and then we want spring framework to create the object which is actually not happening because if I run this this is

00:32

what you are getting we were getting the error which says premature end of file uh which file I'm talking about is the spring.xml because if you want spring to create the object we have to do the configuration and this configuration will be done in the XML file and this is empty at this point I don't want this to

00:49

be empty we have to write some configuration using which you can create the object and if you have this question in your mind why we are using XML when we are happy with spring boot there are two reasons why one is what if your project which you are maintaining is a legacy project in that case of course

01:04

they will be having or they might be having XML configuration so you should know second is if you want to understand what is happening behind this INF for spring boot this configuration will help you there okay so let's get started now if you want to do this configuration even before that when you talk about

01:19

this line number uh 10 here and when you say you are creating a context behind the scene we got the container so we do have a container already but in this container we don't have the object yet so if you want to create the object or if you want spring to create the object you have to mention that here and every

01:37

class which spring manages they're called Spring beans right or managed beans so what I will do is since we want to create beans I will use a beans tag now this is how you work with XML if you're new to XML basically you can compare XML with HTML uh HTML stands for

01:52

hypertext markup language XML stands for extensible markup language so basically in the HTML you have some tags which you can use and who understands those tags is your browser so in the browser it is it has a definition of all those tags

02:08

which you're using maybe you're using HTML tag body tag or a P tag or a div tag so basically your browser knows those tags because they have the definition of it in XML you can use any tag which you want there's no compulsion that you should use this tag or those

02:25

tags even if I want I can use the naven tag here I mean that's my choice I want to use n tag I can use that but when it comes to Spring framework we have to use certain tags which are which has their own definitions and we do that with the help of DTD files which is document type

02:42

defin definitions you don't have to get into that much just imagine XML you can use your own tags provided you have a definitions for it now we are using some tags here and we do have a definition of definitions for it and I will show you

02:57

that in sometime uh but we have to use a bean tag because you'll be having multiple classes and multiple objects and inside this Bean tag you can create multiple Bean tag right so you can have one depend upon your requirement you can have multiple Bean now in this Bean you can mention which

03:13

class Bean you want which class object or which class you want spring to manage so I can mention the class name here with the help of class attribute and you can mention the class name which is Dev in this case now simply class name will not work you have to basically provide

03:29

the qualified name for it so in this case it is com. theis dodev which is the qualified name so you can see Dev belongs to com tcod as a package name and then optionally you can also provide the ID for it so let's say ID is Dev you can use any name doesn't matter I'm

03:45

using Dev in this case now where this will be useful example if you go back to your app in this line number 11 what we doing is we were using uh dev. class in the earlier example you can also use Dev here so earlier basically we we were using dev.

class here we can use in

04:04

double codes we can use Dev but you just have to make sure that since we are using Dev this get bin returns you the object type just typ cast it with Dev and that should work going back here so now we do have a definition right or we do have the bean created so the conf is done let's try so what I will do is I

04:20

will just relaunch this and see what happens okay still it is giving you error it still says uh it is invalid but why this time it is giving you some different error it says line number four which is this beans it says cannot find Declaration of element beans oh this is

04:36

weird so basically uh we don't have a definition for it as I mentioned before you can use any tag provided you have a definition for it for this be we don't have a definition so in this case you don't have to buad definition what you can just do is you can just go to Google and search for spring 6 Bean

04:52

configuration XML and look at this file or this link click here and it talks about the configuration just go down and search for this one and you can simply copy this and replace the beans because in this if you can see there's also Bean tag here or beans tag here just replace

05:08

it and we are good okay means not two times only once and I will just remove this so what we are doing is we are providing the definition for beans here okay and with this if I relaunch it hopefully this

05:24

will work okay hopefully I said but it's not working there's some problem it says line number four there's some issue with the passing some problem with the first line is it the issue okay so maybe there was some issue with this line here okay

05:42

I'll just simply get rid of this maybe there's some extra character which I can't see anyway so if I relaunch this we have provided a ban definition and this is working it says working on the awesome project so by doing this what we are doing is we are asking our spring framework that you are responsible to

05:57

create the object for Dev and which we have done and that's why you are getting this object here so that's our first one how about one more class let's say in this particular project let me create one more class and let me name this class as laptop okay a simple class nothing fancy

06:14

and maybe in this in this class I want to have a method called compile so I will say public void compile and in this I'm going to print compiling and now if I go back to Spring you can see we are just mentioning Dev here right and one more thing what I will do is I will just comment this section now how many

06:32

classes we have in our project of course we have three classes but then we are concerned about the two classes because app only has a main method so we have two classes Dev and laptop which we created a normal class like Dev a normal class like laptop and now if I run this

06:48

tell me what just think about it if I just run this code how many objects will be created inside the container or how many classes object will be created inside the container is it zero is it one is it too so let me know in the comments okay so if I run this let's see

07:04

okay first of all how we will see we we don't even have idea right we are not witing anything see the thing is if you look at the XML file when you execute this line it goes to the XML file in the XML we are creating the object only of one class or we are saying only one bean tag so it will create the object only of

07:22

that class which you have mention here which is Dev okay and to prove that what I will do is I will just have a Constructor here I'll say public Dev which is a Constructor and in this I'm going to print Dev Constructor and I'm going to do the same thing in the laptop

07:38

so I will have a Constructor here of course this should not be Dev it should be laptop and here also I'm going to replace this with laptop okay a simple class simple Constructor nothing fancy okay and now if I try to run this

07:56

so if we don't get any output that means none of the object got created but let's see what happens so we only got Dev Constructor that means we got one object and that to of the developer reason in the SPL XML we have mentioned only that

08:13

what if I create for the laptop as well so I can just uh reuse my code instead of saying copy paste reuse sound skirt and we don't need that much of space between being closing and here I will say laptop and here the class name is laptop and the moment I do that and if I

08:30

run this let's relaunch it and you can see now it says Dev and the laptop as well both got created but we are not using them in the code right we are just saying execute this line create the container and as soon as the container is created it will simply look at the

08:45

XML file and say okay we do got we do have two beans here mentioned let me create the object for them okay so and then in your container you basically have two objects one for your Dev and one for your laptop but the question is what what if you mention the dev one

09:01

more time so I will just copy this and paste it here and let's say Dev one we got different ID so now let me know in the comments how many objects it will create 1 2 3 of course it should be more than two or two or more than two so let's try and you can see it is calling the dev Constructor two times that means

09:18

it it it depends upon how many times you mention be if you say once it will get one object it will if you say two it will say one more time it will create another object for Dev so we got two objects for Dev right on one for your laptop so that's how basically you can configure your

09:35

spring framework to create the object now uh we can go for the next step which is what if in the dev you need object of laptop now that will be tricky right because now your Dev initially we were dependent on the spring container to give you this Dev right Dev object and

09:52

now inside Dev if you want a laptop object now that's where the dependency will come how it will work that will see in the next video so Target for this video is 100 comments see you in the next video bye-bye