Spring bean mod02
-
Upload
guo-albert -
Category
Education
-
view
1.053 -
download
0
Transcript of Spring bean mod02
![Page 1: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/1.jpg)
Spring Ioc
![Page 2: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/2.jpg)
Containing your beans-1
The container is at the core of the Spring Framework
Framework. Spring’s container uses
dependency injection (DI) to manage the components that make up an application.
This includes creating associations between collaborating components
Spring comes with several container
implementations that can be categorized into two distinct types
![Page 3: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/3.jpg)
Containing your beans-2
Bean factories (defined by the
org.springframework.beans.factory.
BeanFactory interface) are the simplest of
containers
![Page 4: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/4.jpg)
Introducing the BeanFactory
As its name implies, a bean factory is an
implementation of the Factory design
pattern. That is, it is a class whose
responsibility is to create and dispense beans
There are several implementations of
BeanFactory in Spring. But the one that is
most commonly used is
org.springframework.beans.factory.xml.
XmlBeanFactory, which loads its beans based on
the definitions contained in an XML file.
![Page 5: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/5.jpg)
Lab-Entity Class(JavaBean Class)
Implements-1規劃一個打招呼的介面interface
/method
![Page 6: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/6.jpg)
Lab-Entity Class(JavaBean Class)
Implements-2
![Page 7: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/7.jpg)
Introducing the BeanFactory
There’s more to a bean factory than simply instantiation and delivery of application objects. Because a bean factory knows about many objects within an application,it is able to create associations between collaborating objects as they are instantiated.
There are several implementations of BeanFactory interface in Spring.
But the one that is most commonly used is org.springframework.beans.factory.xml.
XmlBeanFactory
![Page 8: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/8.jpg)
XmlBeanFactory class
![Page 9: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/9.jpg)
配置 spring bean config
![Page 10: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/10.jpg)
配置Bean
![Page 11: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/11.jpg)
完成的spring bean config
![Page 12: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/12.jpg)
<bean> element
<bean id=“bean識別名稱”
class=“JavaBean class”>
<property name=“setter名稱” value=“屬性值”/>
![Page 13: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/13.jpg)
Lab classpath 參考相對的類別庫
建立lib資料夾
匯入commons.logging-1.1.1-bin.jar/spring-
beans.jar/spring-context.jar/spring-
core.jar
Classpath設定
![Page 14: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/14.jpg)
Test Spring bean Main Program
This simple line of code tells the bean
factory to read the bean definitions from
the XML file
But the bean factory doesn’t instantiate
the beans just yet
![Page 15: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/15.jpg)
Bean Instance
When getBean() is called, the factory will
instantiate the bean and set the bean’s
properties using DI.
![Page 16: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/16.jpg)
Spring Bean in Action-demo
![Page 17: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/17.jpg)
Working with
an application context A bean factory is fine for simple
applications, but to take advantage of the
full power of the Spring Framework, you’ll
probably want to load your application
beans using Spring’s more advanced
container: the application context.
![Page 18: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/18.jpg)
application context?
But an ApplicationContext offers much more…
Application contexts provide a means for resolving text messages, including support for internationalization (I18N) of those messages.
Application contexts provide a generic way to load file resources, such as images.
Application contexts can publish events to beans that are registered as listeners.
![Page 19: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/19.jpg)
ApplicationContext
are three that are commonly used ClassPathXmlApplicationContext
◦ Loads a context definition from an XML file located in the classpath, treating context definition files as classpath resources.
FileSystemXmlApplicationContext
◦ Loads a context definition from an XML file in the file system.
XmlWebApplicationContext
◦ Loads context definitions from an XML file contained within a web application.
![Page 20: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/20.jpg)
FileSystemXmlApplicationContxt
架構
![Page 21: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/21.jpg)
Implements-
FileSystemXmlApplicationContext
![Page 22: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/22.jpg)
FileSystemXmlApplicationContext
Demo
![Page 23: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/23.jpg)
A bean’s life
![Page 24: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/24.jpg)
Injecting through constructors
the xxx class can be constructed in two
different
ways:
■ Using the default constructor
■ Using a constructor that takes an int
argument that indicates the number of
beanbags that the xxxx class
![Page 25: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/25.jpg)
Config constructor Injection-1
Implements Class
![Page 26: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/26.jpg)
Config constructor Injection-2
![Page 27: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/27.jpg)
Constructor Injeciton Main
Programming
![Page 28: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/28.jpg)
Referencing other beans
Kenny’s a very talented instrumentalist
and can play virtually any instrument given
to him.
As long as it implements the Instrument
interface
![Page 29: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/29.jpg)
Bean config
Before we can give Kenny a saxophone to play, we must declare it as a <bean> in Spring. The following XML should do:
With the saxophone declared, we’re ready to give it to Kenny to play. The following modification to the kenny bean uses setter injection to set the instrument property:
![Page 30: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/30.jpg)
Referencing Bean Lab-1
Employee aggregation Working class
Define Working Class
Work interface
![Page 31: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/31.jpg)
Referencing Bean Lab-2
Define Employee class aggregation
Working class
![Page 32: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/32.jpg)
Referencing Bean Lab-3
Spring bean config
![Page 33: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/33.jpg)
Main programming
![Page 34: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/34.jpg)
Injecting inner beans
We’ve seen that Kenny is able to play
saxophone, piano, or any instrument that
implements the Instrument interface. But
what’s also true is that the saxophone and
piano beans could also be shared with any
other bean by injecting them into an
Instrument property.
![Page 35: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/35.jpg)
Inner Injection-lab1
![Page 36: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/36.jpg)
Inner Injection-demo
![Page 37: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/37.jpg)
Wiring collections
use Spring to configure both simple
property values (using the value attribute)
and properties with references to other
beans (using the ref attribute).
Spring offers four types of collection
configuration elements that come in
handy when configuring collections of
values
![Page 38: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/38.jpg)
Collection four types
As for <map> and <props>, these two
elements correspond to collections that
are java.util.Map and java.util.Properties
The <list> and <set> elements are useful
when configuring properties that are
either arrays or some implementation of
java.util.Collection
![Page 39: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/39.jpg)
Spring Collection
Hank’s special talent is that he is a one-
man band. Like Kenny, Hank’s talent is
playing several instruments, but Hank can
play several instruments at the same time
![Page 40: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/40.jpg)
Lists and arrays
To give Hank a collection of instruments
to perform with, let’s use the <list>
configuration element:
![Page 41: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/41.jpg)
Collection Lab-1
一個員工同時間可以進行多樣工作…
規劃一個可以從事多項工作的ChtEmployee Entity Class
![Page 42: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/42.jpg)
Collection Lab-2
Config配置
![Page 43: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/43.jpg)
Collection Lab-3
![Page 44: Spring bean mod02](https://reader034.fdocuments.in/reader034/viewer/2022052523/5550c2dcb4c90504628b553e/html5/thumbnails/44.jpg)
Wiring nothing (null)
you will use DI to wire a value or an
object reference into a bean’s properties.
But what if you want to ensure that a
property is null?
some beans may themselves set a
property to a non-null default value
To set a property to null, you simply use
the <null/> element. For example