π Add to Chrome β Itβs Free - YouTube Summarizer
Category: N/A
No summary available.
00:00
welcome back aliens my name is Zin readyy and in this video we'll talk about spring framework hold on we have talked about spring framework but then when we started coding uh in between we talked about spring Boot and the project which we have created is done with the help of spring Boot and things are bit
00:15
easy right but what if you don't want to use spring boot what if you want to work on a spring framework so there are two reasons why this video is important first what if the project you are working on in your company or maybe by yourself you don't want to use spring
00:30
boot or you don't have a choice of using a spring boot so this video becomes important to understand how do you work with spring framework second reason for this video is in this video you will understand what is happening behind the scene right how spring analyze the configuration and what are different
00:48
aspect of this uh configuration so we'll talk about it of course we have discussed those things in the spring boot as well but when you do it here it will make much more sense so the project which we have here again a simple project what we have done till now is we got uh three different classes we got
01:05
developer we got desktop and laptop and then a developer needs a computer it can be a desktop or laptop and that's what we are doing here but if you see nowhere we have done any configuration basically this the you the spring boot Analyze
01:21
This annotations and then based on that it will create a project for you and it will make it work for you but then as I mentioned there's no XML file there's no upate Java class where you're doing all this configuration so what if you don't want to use springboard I want to use a normal spring framework now so what I
01:37
will do is I will just go back here and create a new project so let's go to here the intellig ID of course any ID will work you will click on new click on project and I don't want to create a spring project now because when you create a spring project it will be
01:53
spring boot project so what I will do is I will click on mavin Arch type so when you talk about a project like spring when you talk about different Frameworks you have to use certain build tool to create those project of course in Spring boot also we have used Maven here also we are going to use mavin of course we
02:08
can also use gdle there uh but let's stick to mavin so here I'm going to create a project I will say demo spring that's my project name and I will keep that in download doesn't matter where it is next the jdk version so make sure that you first of all click on this MAV
02:24
Arc Type or if you're using Eclipse it will give you option of creating a mavin project so select that the jdk word in this machine I have uh 21 I will just go with that and then it is asking you for the catalog see MAV is a build tool also has its way of creating a project it
02:40
will give you a basic structure of a project now based on different framework based on different Services which you want to use they provide you certain Arc types Arc types basically your project structure so they provide you different Arc types which you can use U of course
02:56
you can build this Arc Type or you can build this project from scratch without using their particular uh template but they give you certain templates to use and there are certain templates which they have inbuilt and there are some templates which have been uh sourced so example if I click on catalog here you
03:12
can see there's option of internal then also Maven Central so there are certain templates or Arc types you can use it from MAV Central so if I choose internal here and if I expand this archetype there are limited options you can see uh you can actually at least count it uh there is there's a template for uh A J
03:30
j2e application for portlets for quick start we are going to use Quick Start and we have web app as well but if you click on MAV Central you will see lot of options you can see it is loading here loading done and if I expand now there
03:45
are so many options here you can just sometime it is confusing which one to choose and that's why we are not going to use this maybe there is already application available for spring boot as you can see it is here spring boot 3 rest API Arc Type so you can use this Arc Type it will give you a template to
04:00
work with I will stick to internal and let's create a quick start project so I can just click on quick start here and the version just stick to it there are certain additional properties we can skip that advanced settings this is where you can mention your group ID which is already com. telesco the
04:15
artifact name is demo spring and I can click on create here and I will go I'm going to create a new window because maybe I want to use the old project code so this is my project this is my spring project now now you know what basically
04:32
this is a spring project but only with a name nowhere in the project we are using spring feature see when you say spring feature basically we have to add a dependency for spring and we don't have it here if you can see we don't have a spring dependency we have to add that so
04:47
that's one next if you see the external dependency of course we don't have anything here we don't even have the configuration for spring we have not done for spring boot as well but here we have to create a configuration for to make it work so now what what we will do is let's create a simple code in fact we
05:05
can actually use those codes which we already have so maybe I will just try to reuse so let's go to the spring boot app and I want to use these two classes Dev and uh okay let's only use Dev I'll just copy this and paste it here so we got Dev
05:22
class and we don't need all these annotations now we don't need uh Auto wire other stuff or maybe we don't even need a computer maybe I could have just typed it by myself sometime being lazy is a additional work you get okay so I
05:37
just wanted these two things and I don't even want all these packages a simple class nothing fancy you can see we got a Dev class which has a build method and it says working on the awesome project maybe you can just type it instead of copying it from the older project and now I want to create object of Dev
05:54
inside this app so this is your main code the main Java code first I want to check if this project is running I mean this is the step important step you should do whenever you create a new project without typing any code just run it to check if it is working that's important so here I don't want to print
06:10
hello world uh we just want to okay I just want I don't even want the comments here so what I want here is to create object for Dev so that I can call build so the idea is to call build but for that I have to create object of Dev so I will say Dev obj
06:25
equal to new Dev and then I'm going to say obj do build so this should work because we are creating the object by ourself nowhere we are using spring yet so it says working on the awesome project so we are happy but then we
06:41
don't want to say new jav new Dev here right this is what I want to get from Spring I want spring to create this object now can I use uh we have seen in the spring boot we can can we use spring I mean can we use component here first
06:57
of all we don't have this component option is because we don't have spring boot or spring framework here and even if you use spring framework component it will not able to configure that so this will not work so how do we make it work the first thing is if you want the dev
07:14
here we have to work with the container remember when we have talked about a project where you can have multiple uh classes and then you also have something called a jvm here in which you will be having a section where you'll be be
07:29
having Heap memory and stuff and then inside that you will be having your ioc container and then uh inside this you'll be having objects but unfortunately we don't have the object yet so how do we got get this object so
07:46
even before you get the object the first thing you need to get is the container how will you get the container and we have seen that right before if you want to work with the container we have to create object of application context you need object of application context and we don't have the object first of all we
08:01
don't even have this interface in the project if when I say control space you're not getting the package of it reason this is not part of a Java this is a part of spring framework so if you want this to work we need to get the external libraries for spring and how do
08:16
we do that so we have to add this spring dependency so we already have a jine dependency apart from this we need to get the dependency for spring but not everyone rememberers the dependency name and the artifact ID and the version
08:33
number we don't know that right or maybe some super humans knows uh this by heart I don't know so how do we get it so it's very simple what you can do is you can go to one of the most famous place for the dependency which is called a maven
08:49
repository you can just go here and search for in fact it already had a spring context is it so famous or maybe they are tracking me what I'm doing they got so many uh libraries you know why is only promoting spring maybe even they're tracking me so here basically you have
09:06
to search for spring context and you can see this is what I want so when I click on this I can select any version so normally I prefer the version which doesn't have any vulnerabilities and you can see they give you warning as well 5.3 has the vulnerability we can skip
09:21
that six is a version which we are going for uh which one to choose first of all make sure that you don't have any uh security issue here second go with the version which is not latest but if it is latest and it is used by so many projects that's fine you can go with this 6.1.6 so you can see this version
09:39
basically you can just copy this or if you're not using Maven you can even get it from The Griddle so you can just use that so I'm using Maven I will just copy this and replace this dependency which we have written with the new thing I don't want to Market this spring M repository so yeah so this is how
09:56
basically you create uh you get the dependency oh but it's not coming here why it is not coming here it's because we have to reload the changes so in intj you have to load this mavin changes but in Eclipse it will happen most of the time it happens by default so I will click on
10:12
load Maven changes and now you can say we got the dependencies for spring context and once you have that let's go to our app and let's say control space okay there was some some issue with the importing it should normally work but I don't know what's wrong or maybe I'm
10:29
writing a wrong spelling or maybe inj has some issues anyway so I I got the context I will say this is a this is uh context equal to now how do we create this object so let me just comment this to for time B so how do we create this object so of course you can say new
10:45
application context but application context itself is a interface so you can't get object of application context in that case we can go for the classes which implements application context and one of the class is class path XML application context where going to use this because initially we'll start with
11:01
the XML configuration we can do it with annotations as well or Java configuration let's go with uh XML and that's it you got this object and now once you do this this line basically creates the container so our job is done the container is ready but with that
11:16
container how do I get the object it's very simple you can simply say context. get bean and here you can mention what Bean you want maybe I want the bean of Dev do class our job is done and it should work let's see if that works
11:33
let's run this because we got the container I'm expecting the object is there I just have to use that let's run this and no it's not working so it says bin Factory not insise or already closed so it says before accessing ban why application context first of all what is
11:50
Ban Factory so the way you have application context the container is created with the help of ban Factory it manages the ban the container so in the earlier versions of spring we used to use B Factory but now we use application context so it says something is wrong I mean it's closed I'm not able to create
12:07
the container I mean you're just creating a container but it is closed now so how do you configure your container because in the container we don't have this object of Dev and that's where you have to create a XML configuration but how do we do that so it's simple actually in this bracket you
12:22
can mention the XML configuration name so I will say spring.xml but we have to create this XML file okay and if you done this with just by just by that statement of course this name can be anything it says the XML is not found and where it is searching for
12:39
it it is searching for that file in the class path resource okay uh since it is a class path we have to go with class path so what I will do is in the main folder I will create a directory called resources normally it is there but maybe
12:55
it is not showing that in the intellig idea just check if you have resources folder in this resource folder you will create a file called spring.xml and this is where you have to do the configuration and if you just create a file and if you do nothing here and
13:11
relaunch it this time it should not say file not found it should give you some different error it says line number one column number one premature end of file because we have not done anything but what is this configuration uh let's see that in the next video