spring bean mod02

44
Spring Ioc

Upload: guo-albert

Post on 11-May-2015

1.055 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Spring bean mod02

Spring Ioc

Page 2: Spring bean mod02

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

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

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

Lab-Entity Class(JavaBean Class)

Implements-1規劃一個打招呼的介面interface

/method

Page 6: Spring bean mod02

Lab-Entity Class(JavaBean Class)

Implements-2

Page 7: Spring bean mod02

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

XmlBeanFactory class

Page 9: Spring bean mod02

配置 spring bean config

Page 10: Spring bean mod02

配置Bean

Page 11: Spring bean mod02

完成的spring bean config

Page 12: Spring bean mod02

<bean> element

<bean id=“bean識別名稱”

class=“JavaBean class”>

<property name=“setter名稱” value=“屬性值”/>

Page 13: Spring bean mod02

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

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

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

Spring Bean in Action-demo

Page 17: Spring bean mod02

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

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

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

FileSystemXmlApplicationContxt

架構

Page 21: Spring bean mod02

Implements-

FileSystemXmlApplicationContext

Page 22: Spring bean mod02

FileSystemXmlApplicationContext

Demo

Page 23: Spring bean mod02

A bean’s life

Page 24: Spring bean mod02

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

Config constructor Injection-1

Implements Class

Page 26: Spring bean mod02

Config constructor Injection-2

Page 27: Spring bean mod02

Constructor Injeciton Main

Programming

Page 28: Spring bean mod02

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

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

Referencing Bean Lab-1

Employee aggregation Working class

Define Working Class

Work interface

Page 31: Spring bean mod02

Referencing Bean Lab-2

Define Employee class aggregation

Working class

Page 32: Spring bean mod02

Referencing Bean Lab-3

Spring bean config

Page 33: Spring bean mod02

Main programming

Page 34: Spring bean mod02

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

Inner Injection-lab1

Page 36: Spring bean mod02

Inner Injection-demo

Page 37: Spring bean mod02

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

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

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

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

Collection Lab-1

一個員工同時間可以進行多樣工作…

規劃一個可以從事多項工作的ChtEmployee Entity Class

Page 42: Spring bean mod02

Collection Lab-2

Config配置

Page 43: Spring bean mod02

Collection Lab-3

Page 44: Spring bean mod02

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