Texantek » Core Java

Bean Scope in Spring  XML

Author Message
Anonymous



In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller.There are 5 types of bean scopes that spring supports:
singleton – Return a single bean instance per Spring IoC container
prototype – Return a new bean instance each time when requested
request – Return a single bean instance per HTTP request. *
session – Return a single bean instance per HTTP session. *
globalSession – Return a single bean instance per global HTTP session. *

In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton
An Example showing the difference between singleton and prototype is presented as under:


Singleton Scope example

If no bean scope is specified in bean configuration file, it defaults to singleton. Below is the configuration file "Spring-Customer.xml"

Now when we run the program we get the following output:
The test program is as under:

Now run the program:
The output is as under:

Since the bean ‘customerService’ is in singleton scope, the second retrieval by ‘custB’ will display the message set by ‘custA’ also, even it’s retrieve by a new getBean() method. In singleton, only a single instance per Spring IoC container, no matter how many time you retrieve it with getBean(), it will always return the same instance.
Prototype Example
update the "Spring-Customer.xml" file to define its scope as prototype as under:

When you run the test program again. the output is as under:

In prototype scope, you will have a new instance for each getBean() method called.
you can also use annotations to define bean scope

Now enable auto component scanning in the "spring-customer.xml"file as under:
Send a PM
Rivero



Joined: 22/11/2017 03:05:40
Messages: 1
Offline

Excellent examples. This definitely clears up some stuff for me.
Send a PM
Meeniwo9471



Joined: 18/04/2018 22:09:35
Messages: 1
Offline

jordan 11 win like 82
converse outlet
barbour
fitflops sale clearance
birkenstocks
polo ralph lauren outlet
skechers outlet
kobe 12
nike clearance
asics gel lyte
curry 4
yeezy boost 350 v2
jordan 12
pandora charm
ecco shoes for men
jordan retro 12
asics shoes
mbt shoes clearance
cheap nfl jerseys china
yeezy shoes
canadian goose jacket
win like 96
louboutin shoes
longchamp bags
red bottoms
yeezy beluga 2.0
nike air vapormax
fitflops
beats headphones
pandora bracelets clearance
calvin klein underwear
nike air vapormax
pandora bracelets clearance
louboutin mens
mbt
pandora rings
adidas nmd
balenciaga shoes
fake yeezys
the north face jackets
pandora uk
jordan 11 red
rihanna puma
the north face outlet online
yeezy zebra
air max plus
louboutin
timberland boots
red bottom
pandora jewelry
pandora sale
yeezy boost 350 v2
red jordan 11
timberland outlet stores
north face clearance
jordan 11 red
yeezy boost 350 v2
nike air max 2018
kd 10 shoes
christian louboutin mens shoes
ecco
adidas yeezy
yeezy zebra v2
birkenstock shoes
nike vapor max plus
timberland outlet stores
red bottom shoes for women
air jordan retro 11
ralph lauren sale clearance uk
calvin klein outlet
michael kors outlet clearance
coach handbags outlet
salvatore ferragamo shoes
soccer cleats
superdry uk
fitflop
cheap nfl jerseys
pandora jewelry official site
clarks shoes for women
adidas yeezy
supreme hoodie
yeezy boost
vapor max
barbour jackets
timberland
vapor max
pandora
jimmy choo outlet
kobe bryant shoes
pandora bracelet
birkenstock sandals
yeezy
supremenewyork
yeezy boost
red bottom heels
skechers uk
nike factory store
under armour clearance
vapormax flyknit
adidas outlet
christian louboutin
timberland outlet
fake rolex watches
under armour outlet online
curry shoes
christian louboutin shoes
coach outlet
michael kors handbags outlet
pandora ring
vapormax plus
air max plus
red bottom shoes
pandora
curry 4 shoes
birkin bag
adidas outlet store
pandora earrings
yeezy beluga
pandora jewelry
beats wireless headphones
red bottoms for men
air max 97
pandora charms
asics running shoes
fitflop
red bottom
louboutin outlet
pandora bracelet
lebron 15
balenciaga triple s
pandora jewelry
adidas ultra boost
canadian goose
adidas yeezy
wholesale jerseys china
nike vapor max
pandora jewelry
huarache shoes
pandora rings
jordan retro
lacoste polo shirts
pandora charms sale
kd 10
louboutin shoes
nike vapor max
air max 2018
beats headphones
birkenstock outlet
kyrie irving shoes
timberland shoes
ferragamo shoes
fitflop sandals
pandora jewelry
beats wireless
blue tint yeezy
north face uk
adidas yeezy boost
pandora charms
jordan 11 win like 96
ralph lauren factory store
skechers shoes
fake yeezy
birkenstock outlet
pandora charms
louboutin boots
christian louboutin men
north face jackets
salvatore ferragamo
nike factory
win like 82
pandora bracelet charms
fenty puma
lebron 15 shoes
canada goose jackets
red bottoms louboutin
nike outlet online
calvin klein online
birkin bags
cheap nike
pandora jewelry outlet
pandora charms
pandora bracelet
nike outlet store
barbour uk
ultra boost
replica watches
birkenstock sandals
nike uk
nike kyrie
red bottom shoes
cheap soccer cleats
pandora jewelry 70 off clearance
skechers shoes
converse shoes
lacoste outlet
kyrie 4
red 11s
fake yeezy boost
nike air max plus
longchamp handbags
pandora jewelry official site
yeezy blue tint
wholesale nfl jerseys
pandora necklace
louboutin shoes
nmd r1
kyrie 3
canada goose sale
clarks shoes
barbour jackets
huaraches nike
valentino shoes
replica rolex
supreme clothing
timberland uk
goyard
asics gel
vapor max
jimmy choo shoes
north face jackets clearance
gym red 11
valentino outlet
air max 2018
goyard bags
balenciaga sneakers
pandora charms
clarks shoes for women
yeezy boost 350 v2
rihanna fenty puma
2018.0522W
Send a PM
 
Texantek » Core Java
Go to:   
Powered by JForum © Texantek