#8 Spring without Boot
Summary
TLDRIn this educational video, the host Zin delves into the intricacies of the Spring Framework, contrasting it with Spring Boot. The video aims to guide viewers on how to work with Spring without the convenience of Spring Boot, emphasizing the importance of understanding underlying configurations. Zin demonstrates creating a project with Maven, adding Spring dependencies, and setting up an ApplicationContext to manage beans, highlighting the necessity of XML configuration for non-Spring Boot projects.
Takeaways
- 🚀 The video discusses transitioning from Spring Boot to the traditional Spring Framework for those who don't want or can't use Spring Boot.
- 🔍 The importance of understanding the Spring Framework's configuration and what happens behind the scenes is highlighted.
- 🛠 The video provides a step-by-step guide to creating a new project without Spring Boot, using Maven as the build tool.
- 📚 It's explained that Spring dependencies need to be manually added to a project not using Spring Boot, unlike in Spring Boot where they are auto-configured.
- 💡 The process of creating a simple Spring project is demonstrated, starting from a basic 'Dev' class without annotations.
- 🔑 The necessity of obtaining the ApplicationContext to work with the Spring container is emphasized for object creation and dependency injection.
- 📝 The video mentions the use of XML configuration files for defining beans and their relationships in the Spring container.
- 🔄 The importance of reloading Maven changes to update project dependencies is covered.
- 📁 The creation of a 'spring.xml' file in the 'resources' directory is necessary for configuring the Spring container.
- 🛑 The video points out common errors such as 'BeanFactory not initialized or already closed', indicating issues with the container setup.
- 🔍 The script ends with an introduction to XML configuration, promising further details in the next video.
Q & A
What is the main purpose of the video?
-The main purpose of the video is to explain how to work with the Spring Framework without using Spring Boot, and to understand what happens behind the scenes in Spring's configuration.
Why might someone choose not to use Spring Boot?
-Some might choose not to use Spring Boot due to project requirements or personal preference, necessitating a deeper understanding of the traditional Spring Framework setup.
What is the difference between Spring Boot and the traditional Spring Framework?
-Spring Boot simplifies the setup and development of Spring applications by automatically configuring based on annotations, whereas the traditional Spring Framework requires manual configuration through XML or Java-based configuration.
What is the first step in creating a new project without Spring Boot?
-The first step is to create a new Maven project without the Spring Boot dependencies and then manually add the necessary Spring dependencies.
What is the role of the 'application context' in the Spring Framework?
-The application context is the central part of the Spring Framework that manages the beans and their dependencies, serving as the container for the Spring application.
How does one obtain the Spring dependencies for a project?
-One can obtain the Spring dependencies by searching for them on the Maven Repository and adding the appropriate dependency to the project's Maven configuration.
What is the purpose of the 'context.getBean' method?
-The 'context.getBean' method is used to retrieve beans from the application context, allowing the Spring Framework to manage the creation and lifecycle of objects.
Why is it necessary to create an XML configuration file for the Spring Framework?
-An XML configuration file is necessary to define the beans and their relationships in the Spring Framework, as it does not automatically configure these aspects like Spring Boot does.
What error might one encounter when trying to access a bean before the application context is properly set up?
-One might encounter an error stating that the bean factory is not initialized or already closed, indicating that the application context has not been correctly configured or is not accessible.
What is the typical location for the XML configuration file in a Spring project?
-The typical location for the XML configuration file is in the 'resources' directory of the project, which is part of the classpath.
What is the next step after creating the application context and XML configuration?
-The next step is to define the beans and their properties in the XML configuration file and then test the application to ensure that the beans are being managed correctly by the Spring Framework.
Outlines
😀 Introduction to Spring Framework without Spring Boot
In this introductory segment, the speaker, Zin, welcomes viewers back and outlines the purpose of the video, which is to discuss the Spring Framework in the context of not using Spring Boot. The speaker emphasizes the importance of understanding the Spring Framework for those who cannot or prefer not to use Spring Boot in their projects. The video aims to clarify what happens behind the scenes in Spring, such as configuration analysis and different aspects of the framework. The speaker introduces a simple project involving three classes: Developer, Desktop, and Laptop, and explains how Spring Boot simplifies the process without the need for XML configuration or a separate Java class for configuration.
👨💻 Transitioning from Spring Boot to Traditional Spring Framework
The speaker proceeds to demonstrate how to transition from using Spring Boot to the traditional Spring Framework by creating a new Maven project without Spring Boot features. They guide viewers through the process of selecting a Maven Archetype, choosing a Quick Start project, and setting up the basic project structure. The speaker then explains the necessity of adding Spring dependencies manually, as they are not included by default in a Maven project. They also discuss the importance of creating an ApplicationContext object to work with the Spring container and the steps involved in setting up the project to use Spring without annotations or automatic configuration provided by Spring Boot.
🔧 Setting Up Spring Context and Resolving Configuration Issues
In this part, the speaker focuses on setting up the Spring context and addressing potential configuration issues. They explain how to obtain the necessary Spring dependencies from the Maven repository and add them to the project. The speaker demonstrates how to create an ApplicationContext instance using ClassPathXmlApplicationContext and access beans from the container using the getBean method. However, they encounter an issue where the container appears to be closed, indicating a problem with the configuration. The speaker suggests that the issue might be related to the timing of accessing the beans before the container is properly initialized. They conclude by indicating that the next step will be to create an XML configuration file to resolve the issue.
Mindmap
Keywords
💡Spring Framework
💡Spring Boot
💡Configuration
💡IoC Container
💡ApplicationContext
💡XML Configuration
💡Maven
💡Dependency
💡Bean
💡Maven Repository
💡Classpath
Highlights
Introduction to the importance of understanding the Spring Framework without Spring Boot.
Explanation of two reasons why the video is important: working without Spring Boot and understanding configuration behind the scenes.
Overview of a simple project setup with three classes: Developer, Desktop, and Laptop.
Discussion on the absence of XML configuration and Java update classes in Spring Boot projects.
Demonstration of creating a new project in IntelliJ IDEA without Spring Boot.
Explanation of Maven archetypes and their role in project structure.
Selection of a Maven archetype for a quick start project.
Instructions on setting up a Maven project in Eclipse or IntelliJ IDEA.
Importance of adding Spring dependencies to a project manually when not using Spring Boot.
Guidance on how to find and add Spring context dependency from the Maven repository.
Process of creating a simple class without Spring annotations for a pure Spring Framework setup.
Explanation of creating an object of Developer class and invoking its method without Spring's dependency injection.
Introduction to the concept of ApplicationContext and its necessity for Spring Framework.
Demonstration of creating an ApplicationContext object for the Spring container.
Discussion on the error encountered when trying to access the bean before the ApplicationContext is ready.
Explanation of BeanFactory and its role in managing the Spring container.
Introduction to XML configuration for defining beans in the Spring container.
Instruction on creating a 'spring.xml' file for the project's Spring configuration.
Conclusion and预告 of the next video to cover the XML configuration in detail.
Transcripts
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
5.0 / 5 (0 votes)