#8 Spring without Boot

Telusko
9 May 202413:25

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

00:00

πŸ˜€ 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.

05:00

πŸ‘¨β€πŸ’» 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.

10:02

πŸ”§ 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 Framework is an open-source Java platform that provides comprehensive infrastructure support for developing Java applications. It is the foundation upon which Spring Boot is built. In the video, the speaker discusses the scenario where one might choose to use Spring Framework instead of Spring Boot, explaining the configuration and setup process without the convenience of Spring Boot's auto-configuration.

πŸ’‘Spring Boot

Spring Boot is a project that simplifies the bootstrapping and development of new Spring applications. It favors convention over configuration and is designed to get you up and running as quickly as possible. The script contrasts Spring Boot with the traditional Spring Framework, highlighting the differences in project setup and configuration.

πŸ’‘Configuration

In the context of the video, configuration refers to the setup and arrangement of components within a Spring application. The speaker explains how Spring Boot handles configuration automatically through annotations, whereas with Spring Framework, manual configuration is required, which is demonstrated by creating an XML file for bean definitions.

πŸ’‘IoC Container

The Inversion of Control (IoC) container is a fundamental part of the Spring Framework, which manages the creation and lifecycle of application components. The video script discusses the need to obtain an IoC container to manage the objects in a Spring application, which is done by creating an instance of 'ApplicationContext'.

πŸ’‘ApplicationContext

ApplicationContext is the central class in the Spring Framework used to represent the IoC container. It is responsible for instantiating, configuring, and assembling the beans. The script describes the process of creating an ApplicationContext instance to access the IoC container and retrieve beans like the 'Dev' object.

πŸ’‘XML Configuration

XML configuration is a way to define beans and their relationships in a Spring application using XML files. The speaker in the video decides to use XML configuration to manually set up the Spring Framework project, as opposed to relying on Spring Boot's annotation-based configuration.

πŸ’‘Maven

Maven is a build automation tool used primarily for Java projects. It is utilized in the script to manage the project's build process and dependencies. The speaker mentions Maven when discussing the creation of a new project and the addition of Spring Framework dependencies.

πŸ’‘Dependency

In the context of Maven and the video, a dependency refers to an external library or module that a project requires to function. The script includes a step where the speaker adds the Spring Framework dependency to the project's Maven configuration to incorporate Spring features.

πŸ’‘Bean

In Spring, a bean is an object that is instantiated, assembled, and managed by the IoC container. The video script explains the process of defining beans in an XML configuration file and retrieving them from the ApplicationContext using the 'getBean' method.

πŸ’‘Maven Repository

The Maven Repository is a central place to manage and share Java artifacts. The speaker in the video uses the Maven Repository to find and copy the dependency information for the Spring Framework, which is essential for adding Spring features to a Maven-managed project.

πŸ’‘Classpath

The classpath is a parameter that tells the Java Virtual Machine or a Java compiler where to look for user-defined classes and packages. In the script, the speaker mentions the classpath when discussing the location where the Spring XML configuration file should be placed so that it can be found by the ApplicationContext.

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

play00:00

welcome back aliens my name is Zin

play00:01

readyy and in this video we'll talk

play00:03

about spring framework hold on we have

play00:05

talked about spring framework but then

play00:07

when we started coding uh in between we

play00:09

talked about spring Boot and the project

play00:11

which we have created is done with the

play00:13

help of spring Boot and things are bit

play00:15

easy right but what if you don't want to

play00:18

use spring boot what if you want to work

play00:20

on a spring framework so there are two

play00:23

reasons why this video is important

play00:25

first what if the project you are

play00:26

working on in your company or maybe by

play00:29

yourself you don't want to use spring

play00:30

boot or you don't have a choice of using

play00:32

a spring boot so this video becomes

play00:34

important to understand how do you work

play00:36

with spring framework second reason for

play00:38

this video is in this video you will

play00:41

understand what is happening behind the

play00:42

scene right how spring analyze the

play00:45

configuration and what are different

play00:48

aspect of this uh configuration so we'll

play00:51

talk about it of course we have

play00:53

discussed those things in the spring

play00:54

boot as well but when you do it here it

play00:57

will make much more sense so the project

play00:59

which we have here again a simple

play01:00

project what we have done till now is we

play01:02

got uh three different classes we got

play01:05

developer we got desktop and laptop and

play01:08

then a developer needs a computer it can

play01:11

be a desktop or laptop and that's what

play01:12

we are doing here but if you see nowhere

play01:15

we have done any configuration basically

play01:18

this the you the spring boot Analyze

play01:21

This annotations and then based on that

play01:23

it will create a project for you and it

play01:25

will make it work for you but then as I

play01:27

mentioned there's no XML file there's no

play01:29

upate Java class where you're doing all

play01:31

this configuration so what if you don't

play01:33

want to use springboard I want to use a

play01:35

normal spring framework now so what I

play01:37

will do is I will just go back here and

play01:40

create a new project so let's go to here

play01:44

the intellig ID of course any ID will

play01:45

work you will click on new click on

play01:48

project and I don't want to create a

play01:50

spring project now because when you

play01:51

create a spring project it will be

play01:53

spring boot project so what I will do is

play01:55

I will click on mavin Arch type so when

play01:58

you talk about a project like spring

play01:59

when you talk about different Frameworks

play02:01

you have to use certain build tool to

play02:03

create those project of course in Spring

play02:04

boot also we have used Maven here also

play02:06

we are going to use mavin of course we

play02:08

can also use gdle there uh but let's

play02:10

stick to mavin so here I'm going to

play02:12

create a project I will say demo spring

play02:16

that's my project name and I will keep

play02:18

that in download doesn't matter where it

play02:19

is next the jdk version so make sure

play02:22

that you first of all click on this MAV

play02:24

Arc Type or if you're using Eclipse it

play02:26

will give you option of creating a mavin

play02:27

project so select that the jdk word in

play02:30

this machine I have uh 21 I will just go

play02:33

with that and then it is asking you for

play02:35

the catalog see MAV is a build tool also

play02:38

has its way of creating a project it

play02:40

will give you a basic structure of a

play02:42

project now based on different framework

play02:45

based on different Services which you

play02:46

want to use they provide you certain Arc

play02:49

types Arc types basically your project

play02:51

structure so they provide you different

play02:53

Arc types which you can use U of course

play02:56

you can build this Arc Type or you can

play02:57

build this project from scratch without

play02:59

using their particular uh template but

play03:03

they give you certain templates to use

play03:04

and there are certain templates which

play03:06

they have inbuilt and there are some

play03:08

templates which have been uh sourced so

play03:10

example if I click on catalog here you

play03:12

can see there's option of internal then

play03:15

also Maven Central so there are certain

play03:17

templates or Arc types you can use it

play03:18

from MAV Central so if I choose internal

play03:21

here and if I expand this archetype

play03:22

there are limited options you can see uh

play03:24

you can actually at least count it uh

play03:27

there is there's a template for uh A J

play03:30

j2e application for portlets for quick

play03:33

start we are going to use Quick Start

play03:35

and we have web app as well but if you

play03:38

click on MAV Central you will see lot of

play03:41

options you can see it is loading here

play03:42

loading done and if I expand now there

play03:45

are so many options here you can just

play03:48

sometime it is confusing which one to

play03:49

choose and that's why we are not going

play03:50

to use this maybe there is already

play03:52

application available for spring boot as

play03:54

you can see it is here spring boot 3

play03:56

rest API Arc Type so you can use this

play03:58

Arc Type it will give you a template to

play04:00

work with I will stick to internal and

play04:02

let's create a quick start project so I

play04:04

can just click on quick start here and

play04:06

the version just stick to it there are

play04:08

certain additional properties we can

play04:10

skip that advanced settings this is

play04:12

where you can mention your group ID

play04:13

which is already com. telesco the

play04:15

artifact name is demo spring and I can

play04:18

click on create here and I will go I'm

play04:21

going to create a new window because

play04:23

maybe I want to use the old project code

play04:26

so this is my project this is my spring

play04:28

project now now you know what basically

play04:32

this is a spring project but only with a

play04:35

name nowhere in the project we are using

play04:38

spring feature see when you say spring

play04:39

feature basically we have to add a

play04:41

dependency for spring and we don't have

play04:44

it here if you can see we don't have a

play04:45

spring dependency we have to add that so

play04:47

that's one next if you see the external

play04:51

dependency of course we don't have

play04:52

anything here we don't even have the

play04:54

configuration for spring we have not

play04:56

done for spring boot as well but here we

play04:58

have to create a configuration for to

play05:00

make it work so now what what we will do

play05:02

is let's create a simple code in fact we

play05:05

can actually use those codes which we

play05:06

already have so maybe I will just try to

play05:09

reuse so let's go to the spring boot app

play05:12

and I want to use these two classes Dev

play05:15

and uh okay let's only use Dev I'll just

play05:18

copy

play05:19

this and paste it here so we got Dev

play05:22

class and we don't need all these

play05:24

annotations now we don't need uh Auto

play05:27

wire other stuff or maybe we don't even

play05:30

need a computer maybe I could have just

play05:32

typed it by myself sometime being lazy

play05:34

is a additional work you get okay so I

play05:37

just wanted these two things and I don't

play05:39

even want all these packages a simple

play05:41

class nothing fancy you can see we got a

play05:43

Dev class which has a build method and

play05:46

it says working on the awesome project

play05:48

maybe you can just type it instead of

play05:49

copying it from the older project and

play05:51

now I want to create object of Dev

play05:54

inside this app so this is your main

play05:56

code the main Java code first I want to

play05:58

check if this project is running I mean

play06:00

this is the step important step you

play06:01

should do whenever you create a new

play06:03

project without typing any code just run

play06:05

it to check if it is working that's

play06:07

important so here I don't want to print

play06:10

hello world uh we just want to okay I

play06:13

just want I don't even want the comments

play06:14

here so what I want here is to create

play06:16

object for Dev so that I can call build

play06:18

so the idea is to call

play06:20

build but for that I have to create

play06:22

object of Dev so I will say Dev obj

play06:25

equal to new Dev and then I'm going to

play06:28

say obj do build so this should work

play06:32

because we are creating the object by

play06:34

ourself nowhere we are using spring yet

play06:37

so it says working on the awesome

play06:39

project so we are happy but then we

play06:41

don't want to say new jav new Dev here

play06:44

right this is what I want to get from

play06:45

Spring I want spring to create this

play06:48

object now can I use uh we have seen in

play06:51

the spring boot we can can we use spring

play06:54

I mean can we use component here first

play06:57

of all we don't have this component

play06:59

option is because we don't have spring

play07:00

boot or spring framework here and even

play07:03

if you use spring framework component it

play07:07

will not able to configure that so this

play07:08

will not work so how do we make it work

play07:11

the first thing is if you want the dev

play07:14

here we have to work with the container

play07:16

remember when we have talked about a

play07:18

project where you can have multiple uh

play07:21

classes and then you also have something

play07:24

called a jvm here in which you will be

play07:27

having a section where you'll be be

play07:29

having Heap memory and stuff and then

play07:31

inside that you will be having your

play07:35

ioc container and then uh inside this

play07:39

you'll be having objects but

play07:40

unfortunately we don't have the object

play07:43

yet so how do we got get this object so

play07:46

even before you get the object the first

play07:47

thing you need to get is the container

play07:49

how will you get the container and we

play07:51

have seen that right before if you want

play07:52

to work with the container we have to

play07:54

create object of application context you

play07:57

need object of application context and

play07:59

we don't have the object first of all we

play08:01

don't even have this interface in the

play08:03

project if when I say control space

play08:06

you're not getting the package of it

play08:07

reason this is not part of a Java this

play08:10

is a part of spring framework so if you

play08:12

want this to work we need to get the

play08:14

external libraries for spring and how do

play08:16

we do that so we have to add this spring

play08:20

dependency so we already have a jine

play08:22

dependency apart from this we need to

play08:24

get the dependency for spring but not

play08:28

everyone rememberers the dependency name

play08:31

and the artifact ID and the version

play08:33

number we don't know that right or maybe

play08:35

some super humans knows uh this by heart

play08:40

I don't know so how do we get it so it's

play08:42

very simple what you can do is you can

play08:44

go to one of the most famous place for

play08:47

the dependency which is called a maven

play08:49

repository you can just go here and

play08:51

search for in fact it already had a

play08:54

spring context is it so famous or maybe

play08:57

they are tracking me what I'm doing they

play09:00

got so many uh libraries you know why is

play09:02

only promoting spring maybe even they're

play09:04

tracking me so here basically you have

play09:06

to search for spring context and you can

play09:08

see this is what I want so when I click

play09:10

on this I can select any version so

play09:12

normally I prefer the version which

play09:14

doesn't have any vulnerabilities and you

play09:16

can see they give you warning as well

play09:18

5.3 has the vulnerability we can skip

play09:21

that six is a version which we are going

play09:23

for uh which one to choose first of all

play09:25

make sure that you don't have any uh

play09:27

security issue here second go with the

play09:29

version which is not latest but if it is

play09:32

latest and it is used by so many

play09:34

projects that's fine you can go with

play09:36

this 6.1.6 so you can see this version

play09:39

basically you can just copy this or if

play09:40

you're not using Maven you can even get

play09:42

it from The Griddle so you can just use

play09:44

that so I'm using Maven I will just copy

play09:46

this and replace this dependency which

play09:48

we have written with the new thing I

play09:51

don't want to Market this spring M

play09:53

repository so yeah so this is how

play09:56

basically you create uh you get the

play09:58

dependency

play09:59

oh but it's not coming here why it is

play10:02

not coming here it's because we have to

play10:04

reload the changes so in intj you have

play10:07

to load this mavin changes but in

play10:09

Eclipse it will happen most of the time

play10:10

it happens by default so I will click on

play10:12

load Maven changes and now you can say

play10:15

we got the dependencies for spring

play10:17

context and once you have that let's go

play10:20

to our app and let's say control space

play10:23

okay there was some some issue with the

play10:25

importing it should normally work but I

play10:27

don't know what's wrong or maybe I'm

play10:29

writing a wrong spelling or maybe inj

play10:31

has some issues anyway so I I got the

play10:33

context I will say this is a this is uh

play10:36

context equal to now how do we create

play10:39

this object so let me just comment this

play10:41

to for time B so how do we create this

play10:43

object so of course you can say new

play10:45

application context but application

play10:47

context itself is a interface so you

play10:50

can't get object of application context

play10:51

in that case we can go for the classes

play10:53

which implements application context and

play10:56

one of the class is class path XML

play10:58

application context where going to use

play10:59

this because initially we'll start with

play11:01

the XML configuration we can do it with

play11:03

annotations as well or Java

play11:05

configuration let's go with uh XML and

play11:08

that's it you got this object and now

play11:10

once you do this this line basically

play11:12

creates the container so our job is done

play11:14

the container is ready but with that

play11:16

container how do I get the object it's

play11:18

very simple you can simply say context.

play11:21

get bean and here you can mention what

play11:24

Bean you want maybe I want the bean of

play11:27

Dev do class our job is done and it

play11:30

should work let's see if that works

play11:33

let's run this because we got the

play11:35

container I'm expecting the object is

play11:37

there I just have to use that let's run

play11:39

this and no it's not working so it says

play11:43

bin Factory not insise or already closed

play11:47

so it says before accessing ban why

play11:48

application context first of all what is

play11:50

Ban Factory so the way you have

play11:52

application context the container is

play11:54

created with the help of ban Factory it

play11:55

manages the ban the container so in the

play11:57

earlier versions of spring we used to

play12:00

use B Factory but now we use application

play12:01

context so it says something is wrong I

play12:04

mean it's closed I'm not able to create

play12:07

the container I mean you're just

play12:08

creating a container but it is closed

play12:10

now so how do you configure your

play12:12

container because in the container we

play12:14

don't have this object of Dev and that's

play12:16

where you have to create a XML

play12:18

configuration but how do we do that so

play12:20

it's simple actually in this bracket you

play12:22

can mention the XML configuration name

play12:24

so I will say spring.xml but we have to

play12:27

create this XML file

play12:29

okay and if you done this with just by

play12:32

just by that statement of course this

play12:34

name can be anything it says the XML is

play12:37

not found and where it is searching for

play12:39

it it is searching for that file in the

play12:42

class path resource okay uh since it is

play12:45

a class path we have to go with class

play12:47

path so what I will do is in the main

play12:49

folder I will create a directory called

play12:53

resources normally it is there but maybe

play12:55

it is not showing that in the intellig

play12:58

idea just check if you have resources

play12:59

folder in this resource folder you will

play13:02

create a file called spring.xml and this

play13:05

is where you have to do the

play13:06

configuration and if you just create a

play13:08

file and if you do nothing here and

play13:11

relaunch it this time it should not say

play13:13

file not found it should give you some

play13:15

different error it says line number one

play13:17

column number one premature end of file

play13:20

because we have not done anything but

play13:21

what is this configuration uh let's see

play13:23

that in the next video

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

5.0 / 5 (0 votes)

Related Tags
Spring FrameworkSpring BootJava DevelopmentConfigurationIOC ContainerMaven ProjectXML SetupCoding TutorialSoftware DevelopmentApplication Context