#9 Spring XML Config

Telusko
10 May 202410:06

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

00:00

πŸ› οΈ 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.

05:01

πŸ“ 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.

10:02

πŸ”„ 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

Spring Framework is an open-source Java platform that provides a comprehensive infrastructure for developing Java applications. In the video, the speaker is discussing the process of creating a Spring project without using Spring Boot, which is a more opinionated and quicker way to set up Spring projects. The theme revolves around manual configuration using XML files, which is a core aspect of traditional Spring applications.

πŸ’‘Configuration

Configuration in the context of the video refers to the setup and specification of how the Spring Framework should manage and create objects within an application. The speaker emphasizes the importance of XML configuration for creating Spring beans, which is a key part of setting up a non-Spring Boot project.

πŸ’‘XML

XML, or Extensible Markup Language, is used in the video to describe the method of configuring the Spring Framework. The speaker explains that XML allows for the definition of custom tags, which in the case of Spring, are used to define beans and their properties. XML configuration is a traditional approach in Spring that contrasts with the more modern, annotation-based configuration in Spring Boot.

πŸ’‘Beans

In the Spring Framework, beans are the objects that are managed by the container. The video script discusses the creation of beans through XML configuration, where each bean corresponds to a class that the Spring container is responsible for instantiating. The term is central to the video's narrative, as it illustrates the manual process of object creation in Spring.

πŸ’‘Dev Class

The 'Dev' class mentioned in the script is a custom class created by the speaker for demonstration purposes. It represents one of the beans that the Spring container is configured to manage. The class is used to illustrate the concept of bean creation and dependency injection within the Spring Framework.

πŸ’‘Constructor

In the context of the video, a constructor is a special method for creating an instance of a class. The speaker adds a constructor to the 'Dev' and 'Laptop' classes to demonstrate the instantiation of objects by the Spring container. The constructor outputs a message when an object is created, helping to verify that the Spring configuration is working as expected.

πŸ’‘Dependency Injection

Dependency Injection is a design pattern in which one object supplies the dependencies of another object. In the video, the speaker hints at the next steps, which will involve the 'Dev' class needing an object of the 'Laptop' class, showcasing how dependencies are managed within the Spring Framework.

πŸ’‘DTD

Document Type Definition (DTD) is referenced when the speaker discusses the validity and definition of custom XML tags used in Spring configuration. While not deeply explored in the script, DTDs are used to define the structure and elements of an XML document, ensuring that the Spring configuration is correctly formatted.

πŸ’‘Legacy Project

A legacy project, as mentioned in the video, is an existing system that was developed at an earlier time and may use older technologies or methodologies. The speaker uses the term to explain one of the reasons for learning XML configuration in Spring, as some legacy projects may still rely on this approach.

πŸ’‘Managed Beans

Managed beans are JavaBeans that are managed by the Spring container. In the script, the speaker refers to the classes defined in the XML as managed beans, emphasizing that Spring is responsible for their lifecycle and dependencies. This term is integral to understanding how the Spring container works with user-defined classes.

πŸ’‘Context

In the Spring Framework, the term 'context' refers to the Spring application context, which is an object that holds the shared state of the application. The speaker mentions creating a context when setting up the project, which is where the Spring container and its beans reside.

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

play00:00

welcome back aliens my name is a ready

play00:02

and let's continue the series on Spring

play00:04

framework now we are basically trying to

play00:05

create a spring project without spring

play00:08

Boot and of course for that we have to

play00:10

do lot of configuration and to achieve

play00:13

that we have basically created the

play00:14

project and in that we do have a Dev

play00:16

class here in which you have a method

play00:18

called build and what we are trying to

play00:19

do is we are trying to call the build

play00:22

but for that we we need object of Dev

play00:25

and then we want spring framework to

play00:27

create the object which is actually not

play00:30

happening because if I run this this is

play00:32

what you are getting we were getting the

play00:33

error which says premature end of file

play00:37

uh which file I'm talking about is the

play00:39

spring.xml because if you want spring to

play00:41

create the object we have to do the

play00:43

configuration and this configuration

play00:45

will be done in the XML file and this is

play00:47

empty at this point I don't want this to

play00:49

be empty we have to write some

play00:50

configuration using which you can create

play00:52

the object and if you have this question

play00:54

in your mind why we are using XML when

play00:56

we are happy with spring boot there are

play00:59

two reasons why one is what if your

play01:01

project which you are maintaining is a

play01:02

legacy project in that case of course

play01:04

they will be having or they might be

play01:06

having XML configuration so you should

play01:07

know second is if you want to understand

play01:10

what is happening behind this INF for

play01:12

spring boot this configuration will help

play01:14

you there okay so let's get started now

play01:17

if you want to do this configuration

play01:18

even before that when you talk about

play01:19

this line number uh 10 here and when you

play01:22

say you are creating a context behind

play01:24

the scene we got the container so we do

play01:26

have a container already but in this

play01:28

container we don't have the object yet

play01:31

so if you want to create the object or

play01:32

if you want spring to create the object

play01:34

you have to mention that here and every

play01:37

class which spring manages they're

play01:38

called Spring beans right or managed

play01:40

beans so what I will do is since we want

play01:43

to create beans I will use a beans tag

play01:45

now this is how you work with XML if

play01:47

you're new to XML basically you can

play01:49

compare XML with HTML uh HTML stands for

play01:52

hypertext markup language XML stands for

play01:55

extensible markup language so basically

play01:58

in the HTML you have some tags which you

play02:01

can use and who understands those tags

play02:04

is your browser so in the browser it is

play02:07

it has a definition of all those tags

play02:08

which you're using maybe you're using

play02:10

HTML tag body tag or a P tag or a div

play02:13

tag so basically your browser knows

play02:16

those tags because they have the

play02:17

definition of it in XML you can use any

play02:21

tag which you want there's no compulsion

play02:23

that you should use this tag or those

play02:25

tags even if I want I can use the naven

play02:28

tag here I mean that's my choice I want

play02:30

to use n tag I can use that but when it

play02:33

comes to Spring framework we have to use

play02:35

certain tags which are which has their

play02:37

own definitions and we do that with the

play02:40

help of DTD files which is document type

play02:42

defin definitions you don't have to get

play02:44

into that much just imagine XML you can

play02:48

use your own tags provided you have a

play02:51

definitions for it now we are using some

play02:53

tags here and we do have a definition of

play02:55

definitions for it and I will show you

play02:57

that in sometime uh but we have to use a

play02:59

bean tag because you'll be having

play03:00

multiple classes and multiple

play03:02

objects and inside this Bean tag you can

play03:05

create multiple Bean tag right so you

play03:07

can have one depend upon your

play03:09

requirement you can have multiple Bean

play03:11

now in this Bean you can mention which

play03:13

class Bean you want which class object

play03:16

or which class you want spring to manage

play03:19

so I can mention the class name here

play03:20

with the help of class attribute and you

play03:22

can mention the class name which is Dev

play03:23

in this case now simply class name will

play03:27

not work you have to basically provide

play03:29

the qualified name for it so in this

play03:31

case it is com. theis dodev which is the

play03:35

qualified name so you can see Dev

play03:37

belongs to com tcod as a package name

play03:39

and then optionally you can also provide

play03:41

the ID for it so let's say ID is Dev you

play03:43

can use any name doesn't matter I'm

play03:45

using Dev in this case now where this

play03:47

will be useful example if you go back to

play03:50

your app in this line number 11 what we

play03:52

doing is we were using uh dev. class in

play03:55

the earlier example you can also use Dev

play03:57

here so earlier basically we we were

play04:00

using dev. class here we can use in

play04:04

double codes we can use Dev but you just

play04:06

have to make sure that since we are

play04:07

using Dev this get bin returns you the

play04:10

object type just typ cast it with Dev

play04:13

and that should work going back here so

play04:15

now we do have a definition right or we

play04:17

do have the bean created so the conf is

play04:19

done let's try so what I will do is I

play04:20

will just relaunch this and see what

play04:22

happens okay still it is giving you

play04:23

error it still says uh it is invalid but

play04:27

why this time it is giving you some

play04:29

different error it says line number four

play04:31

which is this beans it says cannot find

play04:34

Declaration of element beans oh this is

play04:36

weird so basically uh we don't have a

play04:39

definition for it as I mentioned before

play04:41

you can use any tag provided you have a

play04:43

definition for it for this be we don't

play04:44

have a definition so in this case you

play04:47

don't have to buad definition what you

play04:48

can just do is you can just go to Google

play04:50

and search for spring 6 Bean

play04:52

configuration XML and look at this file

play04:54

or this link click here and it talks

play04:56

about the configuration just go down and

play04:59

search for this one and you can simply

play05:01

copy this and replace the beans because

play05:03

in this if you can see there's also Bean

play05:05

tag here or beans tag here just replace

play05:08

it and we are good okay means not two

play05:12

times only

play05:13

once and I will just remove this so what

play05:16

we are doing is we are providing the

play05:18

definition for beans here okay and with

play05:21

this if I relaunch it hopefully this

play05:24

will work okay hopefully I said but it's

play05:27

not working there's some problem it says

play05:30

line number four there's some issue with

play05:32

the passing some problem with the first

play05:35

line is it the issue okay so maybe there

play05:38

was some issue with this line here okay

play05:42

I'll just simply get rid of this maybe

play05:43

there's some extra character which I

play05:45

can't see anyway so if I relaunch this

play05:47

we have provided a ban definition and

play05:49

this is working it says working on the

play05:50

awesome project so by doing this what we

play05:52

are doing is we are asking our spring

play05:54

framework that you are responsible to

play05:57

create the object for Dev and which we

play05:59

have done

play06:00

and that's why you are getting this

play06:03

object here so that's our first one how

play06:05

about one more class let's say in this

play06:07

particular project let me create one

play06:08

more class and let me name this class as

play06:11

laptop okay a simple class nothing fancy

play06:14

and maybe in this in this class I want

play06:16

to have a method called compile so I

play06:17

will say public void compile and in this

play06:20

I'm going to print compiling and now if

play06:23

I go back to Spring you can see we are

play06:25

just mentioning Dev here right and one

play06:28

more thing what I will do is I will just

play06:29

comment this section now how many

play06:32

classes we have in our project of course

play06:34

we have three classes but then we are

play06:36

concerned about the two classes because

play06:37

app only has a main method so we have

play06:39

two classes Dev and laptop which we

play06:41

created a normal class like Dev a normal

play06:43

class like laptop and now if I run this

play06:48

tell me what just think about it if I

play06:50

just run this code how many objects will

play06:53

be created inside the container or how

play06:55

many classes object will be created

play06:56

inside the container is it zero is it

play06:58

one is it too so let me know in the

play07:00

comments okay so if I run this let's see

play07:04

okay first of all how we will see we we

play07:05

don't even have idea right we are not

play07:07

witing anything see the thing is if you

play07:09

look at the XML file when you execute

play07:12

this line it goes to the XML file in the

play07:14

XML we are creating the object only of

play07:16

one class or we are saying only one bean

play07:19

tag so it will create the object only of

play07:22

that class which you have mention here

play07:23

which is Dev okay and to prove that what

play07:26

I will do is I will just have a

play07:28

Constructor here I'll say public Dev

play07:30

which is a Constructor and in this I'm

play07:32

going to print Dev Constructor and I'm

play07:35

going to do the same thing in the laptop

play07:38

so I will have a Constructor here of

play07:40

course this should not be Dev it should

play07:41

be laptop and here also I'm going to

play07:44

replace this with laptop okay a simple

play07:47

class simple Constructor nothing

play07:50

fancy okay and now if I try to run this

play07:56

so if we don't get any output that means

play08:00

none of the object got created but let's

play08:03

see what happens so we only got Dev

play08:05

Constructor that means we got one object

play08:07

and that to of the developer reason in

play08:10

the SPL XML we have mentioned only that

play08:13

what if I create for the laptop as well

play08:15

so I can just uh reuse my code instead

play08:17

of saying copy paste reuse sound skirt

play08:20

and we don't need that much of space

play08:21

between being closing and here I will

play08:24

say laptop and here the class name is

play08:27

laptop and the moment I do that and if I

play08:30

run this let's relaunch it and you can

play08:32

see now it says Dev and the laptop as

play08:34

well both got created but we are not

play08:37

using them in the code right we are just

play08:38

saying execute this line create the

play08:41

container and as soon as the container

play08:42

is created it will simply look at the

play08:45

XML file and say okay we do got we do

play08:47

have two beans here mentioned let me

play08:49

create the object for them okay so and

play08:51

then in your container you basically

play08:53

have two objects one for your Dev and

play08:57

one for your laptop but the question is

play08:59

what what if you mention the dev one

play09:01

more time so I will just copy this and

play09:03

paste it here and let's say Dev one we

play09:05

got different ID so now let me know in

play09:07

the comments how many objects it will

play09:09

create 1 2 3 of course it should be more

play09:12

than two or two or more than two so

play09:14

let's try and you can see it is calling

play09:16

the dev Constructor two times that means

play09:18

it it it depends upon how many times you

play09:20

mention be if you say once it will get

play09:22

one object it will if you say two it

play09:24

will say one more time it will create

play09:26

another object for Dev so we got two

play09:28

objects for Dev

play09:30

right on one for your laptop so that's

play09:33

how basically you can configure your

play09:35

spring framework to create the object

play09:36

now uh we can go for the next step which

play09:39

is what if in the dev you need object of

play09:42

laptop now that will be tricky right

play09:44

because now your Dev initially we were

play09:47

dependent on the spring container to

play09:49

give you this Dev right Dev object and

play09:52

now inside Dev if you want a laptop

play09:55

object now that's where the dependency

play09:57

will come how it will work that will see

play09:59

in the next video so Target for this

play10:01

video is 100 comments see you in the

play10:04

next video bye-bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Spring FrameworkXML ConfigObject CreationLegacy ProjectSpring BeansDependency InjectionDev TutorialCoding SeriesSoftware DevelopmentJava Framework