#9 Spring XML Config
Summary
TLDRIn this instructional video, the presenter guides viewers on setting up a Spring project without Spring Boot, emphasizing the importance of XML configuration. They demonstrate creating a 'Dev' class and configuring it in 'spring.xml' to let Spring manage the object creation. The tutorial also explains the concept of Spring beans and how to define them using XML tags. The presenter further illustrates adding multiple beans and the impact on object creation within the Spring container, setting the stage for a deeper dive into dependency injection in subsequent videos.
Takeaways
- 🚀 The video is a tutorial on creating a Spring project without Spring Boot, emphasizing the manual configuration required for this setup.
- 📝 The speaker discusses the importance of the 'spring.xml' file for object creation in Spring, highlighting the error 'premature end of file' when the file is empty.
- 🔍 The tutorial explains the use of XML configuration in Spring for those maintaining legacy projects or seeking to understand Spring Boot's underlying mechanisms.
- 🌟 XML is introduced as 'Extensible Markup Language', compared to HTML, with the flexibility to use custom tags as long as they are properly defined.
- 📚 The necessity of using specific tags in Spring's XML configuration is mentioned, referencing DTD (Document Type Definitions) for tag definitions.
- 🌱 The concept of 'Spring beans' or 'managed beans' is introduced, which are classes managed by the Spring container.
- 🏷️ The tutorial demonstrates how to define a bean in the XML file using the 'bean' tag, including specifying the class name with its fully qualified name.
- 🔄 The video shows troubleshooting steps for configuration errors, such as missing definitions for custom tags, by using online resources to find the correct XML structure.
- 🔑 The script explains the use of the 'id' attribute in the 'bean' tag to uniquely identify beans, which can be useful when retrieving them from the Spring container.
- 🔍 The tutorial illustrates that the number of objects created in the Spring container depends on how many 'bean' tags are defined in the XML file.
- 🔗 The video concludes with a teaser for the next topic: managing dependencies between beans, such as a 'Dev' bean needing a 'Laptop' bean.
Q & A
What is the main objective of the video series on Spring Framework?
-The main objective of the video series is to create a Spring project without Spring Boot, focusing on manual configuration and understanding the underlying mechanisms of Spring Framework.
Why is the error 'premature end of file' occurring in the Spring XML configuration?
-The error 'premature end of file' occurs because the Spring XML configuration file is empty and lacks the necessary configuration to instruct Spring to create objects.
What are the two reasons mentioned for using XML configuration in Spring, even when using Spring Boot?
-The two reasons are: 1) to manage legacy projects that use XML configuration, and 2) to understand what happens behind the scenes in Spring Boot's auto-configuration by manually configuring beans in XML.
What is the purpose of the 'beans' tag in Spring XML configuration?
-The 'beans' tag in Spring XML configuration is used to define the context in which Spring will manage the creation and lifecycle of objects, known as Spring beans.
How does one define a Spring bean for a class in the XML configuration?
-A Spring bean for a class is defined by using a 'bean' tag within the 'beans' tag, specifying the fully qualified class name with the 'class' attribute, and optionally providing an 'id' for the bean.
Why is it necessary to provide the fully qualified name of a class when defining a Spring bean?
-The fully qualified name is necessary to uniquely identify the class among potentially many classes with the same simple name but in different packages.
What does the 'id' attribute in a 'bean' tag represent?
-The 'id' attribute in a 'bean' tag represents a unique identifier for the bean, which can be used to reference the bean in the application code or other parts of the configuration.
What is the consequence of running the application without the correct XML configuration?
-Running the application without the correct XML configuration results in errors, such as 'cannot find Declaration of element beans', because Spring cannot understand how to create and manage the required objects.
How does Spring know which tags to use in the XML configuration?
-Spring knows which tags to use in the XML configuration through Document Type Definitions (DTDs) that define the structure and elements allowed in the configuration file.
What happens when multiple 'bean' tags for the same class are defined in the XML configuration?
-When multiple 'bean' tags for the same class are defined, Spring creates multiple instances of that class, each with its own unique identifier if provided, allowing for multiple objects of the same class to be managed by Spring.
What is the significance of the 'constructor' in the context of Spring beans creation?
-The 'constructor' in the context of Spring beans creation is significant because it is called when an instance of the class is created. By observing the constructor output, one can confirm whether the bean instances are being created as expected.
Outlines
🛠️ Setting Up Spring Framework Without Spring Boot
This paragraph introduces the task of creating a Spring project without using Spring Boot, which involves significant manual configuration. The speaker has set up a project with a 'Dev' class containing a 'build' method. However, when attempting to run the project, an error occurs due to the premature end of the 'spring.xml' file, which is empty and requires proper configuration to instruct Spring to create the necessary objects. The speaker also touches on the reasons for using XML configuration, such as dealing with legacy projects or understanding the inner workings of Spring Boot. The explanation includes the concept of Spring beans and how to define them using XML tags, with a focus on the 'beans' tag and the necessity of using specific tags defined by DTD files in the context of the Spring framework.
📝 Resolving Configuration Errors in Spring XML
The speaker continues to troubleshoot the Spring framework configuration by addressing an error indicating that the 'beans' element cannot be found. The solution involves finding the correct XML configuration for Spring beans by searching online and replacing the existing 'beans' tag with the correct one. After making these adjustments, the speaker relaunches the project and confirms that it now works, illustrating the process of creating a 'Dev' object within the Spring container. The paragraph also explores the creation of additional classes, such as 'Laptop', and the implications of running the application with multiple bean definitions, demonstrating how the Spring container creates objects based on the number of bean tags specified in the XML configuration.
🔄 Understanding Object Creation in Spring Container
In this final paragraph, the speaker discusses the behavior of the Spring container when it comes to object creation. By adding constructors to the 'Dev' and 'Laptop' classes that print messages, the speaker tests how many objects are created when the application is run. The results show that the container creates an object for each bean tag mentioned in the XML configuration. The speaker also experiments with duplicating the 'Dev' bean tag to create multiple 'Dev' objects, emphasizing that the number of objects created depends on the number of bean tags. The paragraph concludes with a teaser for the next video, which will delve into dependency management within the Spring framework.
Mindmap
Keywords
💡Spring Framework
💡Configuration
💡XML
💡Beans
💡Dev Class
💡Constructor
💡Dependency Injection
💡DTD
💡Legacy Project
💡Managed Beans
💡Context
Highlights
Introduction to creating a Spring project without Spring Boot, emphasizing manual configuration.
Explanation of the error 'premature end of file' in the spring.xml configuration file.
The necessity of XML configuration in legacy projects or for understanding Spring Boot's inner workings.
Introduction to the 'Dev' class and its 'build' method as a starting point for object creation.
Concept of Spring beans or managed beans in the context of Spring framework.
XML as extensible markup language and its comparison with HTML.
Usage of custom tags in XML and their definitions through DTD files.
The role of 'bean' tags in defining Spring-managed objects.
Specifying the fully qualified class name for Spring bean configuration.
The importance of the 'id' attribute in bean configuration for object identification.
Troubleshooting common XML configuration errors like 'cannot find Declaration of element beans'.
Guidance on finding and using the correct XML configuration for Spring beans.
Demonstration of creating multiple beans for different classes in Spring.
Explanation of the automatic object creation by Spring when the container is initialized.
Discussion on the implications of multiple bean tags for the same class.
Introduction of a new class 'Laptop' and its integration into Spring configuration.
The impact of bean tags on the number of objects created in the Spring container.
Teaser for the next video focusing on dependency management within Spring beans.
Transcripts
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
5.0 / 5 (0 votes)