Spring bean mod02
Upcoming SlideShare
Loading in...5
×
 

Spring bean mod02

on

  • 1,506 views

 

Statistics

Views

Total Views
1,506
Views on SlideShare
1,491
Embed Views
15

Actions

Likes
0
Downloads
21
Comments
0

4 Embeds 15

http://albert-myptc.blogspot.com 10
http://albert-myptc.blogspot.com.br 3
http://albert-myptc.blogspot.in 1
http://albert-myptc.blogspot.com.es 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Spring bean mod02 Spring bean mod02 Presentation Transcript

    • Spring Ioc
    • 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
    • Containing your beans-2  Bean factories (defined by the org.springframework.beans.factory. BeanFactory interface) are the simplest of containers
    • 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.
    • Lab-Entity Class(JavaBean Class) Implements-1  規劃一個打招呼的介面interface /method
    • Lab-Entity Class(JavaBean Class) Implements-2
    • 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
    • XmlBeanFactory class
    • 配置 spring bean config
    • 配置Bean
    • 完成的spring bean config
    • <bean> element  <bean id=“bean識別名稱” class=“JavaBean class”>  <property name=“setter名稱” value=“屬 性值”/>
    • Lab classpath 參考相對的類別庫  建立lib資料夾  匯入commons.logging-1.1.1-bin.jar/spring- beans.jar/spring-context.jar/spring- core.jar  Classpath設定
    • 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
    • Bean Instance  When getBean() is called, the factory will instantiate the bean and set the bean’s properties using DI.
    • Spring Bean in Action-demo
    • 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.
    • 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.
    • 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.
    • FileSystemXmlApplicationContxt 架構
    • Implements- FileSystemXmlApplicationContext
    • FileSystemXmlApplicationContext Demo
    • A bean’s life
    • 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
    • Config constructor Injection-1  Implements Class
    • Config constructor Injection-2
    • Constructor Injeciton Main Programming
    • 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
    • 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:
    • Referencing Bean Lab-1  Employee aggregation Working class  Define Working Class  Work interface
    • Referencing Bean Lab-2  Define Employee class aggregation Working class
    • Referencing Bean Lab-3  Spring bean config
    • Main programming
    • 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.
    • Inner Injection-lab1
    • Inner Injection-demo
    • 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
    • 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
    • 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
    • Lists and arrays  To give Hank a collection of instruments to perform with, let’s use the <list> configuration element:
    • Collection Lab-1  一個員工同時間可以進行多樣工作…  規劃一個可以從事多項工作的 ChtEmployee Entity Class
    • Collection Lab-2  Config配置
    • Collection Lab-3
    • 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