SlideShare a Scribd company logo
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

More Related Content

PPTX
Introduction to Spring Framework
PPTX
Spring framework
PPTX
Spring Framework Presantation Part 1-Core
PPT
Spring introduction
PDF
Java spring framework
PPTX
Introduction to Spring Framework
PDF
Spring Framework - Core
PDF
Spring Framework Training Course
Introduction to Spring Framework
Spring framework
Spring Framework Presantation Part 1-Core
Spring introduction
Java spring framework
Introduction to Spring Framework
Spring Framework - Core
Spring Framework Training Course

What's hot (18)

ODP
Introduction to Spring Framework and Spring IoC
PDF
Spring framework
PDF
Spring Framework
PPTX
Spring Framework Rohit
PPTX
Spring framework in depth
PPTX
Spring MVC framework
ODP
Spring User Guide
PDF
Spring 4 on Java 8 by Juergen Hoeller
PDF
Spring core module
PPTX
Spring Framework
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
ODP
Different Types of Containers in Spring
PDF
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
PPTX
Java Spring Framework
PDF
Spring MVC Framework
PPTX
Building web applications with Java & Spring
PPTX
Next stop: Spring 4
Introduction to Spring Framework and Spring IoC
Spring framework
Spring Framework
Spring Framework Rohit
Spring framework in depth
Spring MVC framework
Spring User Guide
Spring 4 on Java 8 by Juergen Hoeller
Spring core module
Spring Framework
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Different Types of Containers in Spring
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
Java Spring Framework
Spring MVC Framework
Building web applications with Java & Spring
Next stop: Spring 4
Ad

Viewers also liked (20)

PDF
Spring framework core
PPT
Spring + TopLink+JPA + DAO Step by Step
PDF
Spring MVC
PDF
Spring 3 MVC CodeMash 2009
PPT
Spring + JPA + DAO Step by Step
PDF
Spring Framework - Validation
PDF
DBM專案環境建置
PDF
Java7 New Features and Code Examples
PDF
Getting Started with Spring Framework
PPT
Presentation Spring, Spring MVC
PDF
Asynchronous Processing in Java/JEE/Spring
PPTX
Spring Web MVC
PPT
Spring MVC Basics
PPT
Java Persistence API (JPA) Step By Step
PPT
Spring ppt
PDF
Spring Framework - Spring Security
PPT
PDF
JSF2 par la pratique
PDF
Support de cours Spring M.youssfi
PDF
Support JEE Spring Inversion de Controle IOC et Spring MVC
Spring framework core
Spring + TopLink+JPA + DAO Step by Step
Spring MVC
Spring 3 MVC CodeMash 2009
Spring + JPA + DAO Step by Step
Spring Framework - Validation
DBM專案環境建置
Java7 New Features and Code Examples
Getting Started with Spring Framework
Presentation Spring, Spring MVC
Asynchronous Processing in Java/JEE/Spring
Spring Web MVC
Spring MVC Basics
Java Persistence API (JPA) Step By Step
Spring ppt
Spring Framework - Spring Security
JSF2 par la pratique
Support de cours Spring M.youssfi
Support JEE Spring Inversion de Controle IOC et Spring MVC
Ad

Similar to Spring bean mod02 (20)

PPT
Spring talk111204
PDF
Introduction to Spring Framework
PPTX
Skillwise-Spring framework 1
PPT
Spring frame work
PPT
Spring framework
PPT
Spring introduction
PPT
Spring Basics
PPT
Spring training
PPT
SpringIntroductionpresentationoverintroduction.ppt
PPT
Spring
PPT
Spring talk111204
PDF
Manual tutorial-spring-java
PDF
Spring Reference
PDF
Spring Reference
PPT
Spring, web service, web server, eclipse by a introduction sandesh sharma
PPTX
Spring (1)
PDF
Overview chap1
PPTX
Spring Basics
PDF
2-0. Spring ecosytem.pdf
Spring talk111204
Introduction to Spring Framework
Skillwise-Spring framework 1
Spring frame work
Spring framework
Spring introduction
Spring Basics
Spring training
SpringIntroductionpresentationoverintroduction.ppt
Spring
Spring talk111204
Manual tutorial-spring-java
Spring Reference
Spring Reference
Spring, web service, web server, eclipse by a introduction sandesh sharma
Spring (1)
Overview chap1
Spring Basics
2-0. Spring ecosytem.pdf

More from Guo Albert (20)

PPTX
AWS IAM (Identity and Access Management) Policy Simulator
PPTX
TOEIC 準備心得
PDF
JPA Optimistic Locking With @Version
PDF
OCEJPA Study Notes
PPTX
OCEJPA(1Z0-898) Preparation Tips
PPTX
JPA lifecycle events practice
PPTX
XDate - a modern java-script date library
PDF
How to avoid check style errors
PDF
NIG系統報表開發指南
PDF
Ease Your Effort of Putting Data into History Table
PDF
NIG 系統開發指引
PDF
NIG系統開發文件閱讀步驟
PDF
Form Bean Creation Process for NIG System
PDF
A Short Intorduction to JasperReports
PPTX
Apply Template Method Pattern in Report Implementation
PPTX
Utilize Commons BeansUtils to do copy object
PPTX
Apply my eclipse to do entity class generation
PPTX
Nig project setup quickly tutorial
PPTX
Spring JDBCTemplate
PPTX
Java Server Faces + Spring MVC Framework
AWS IAM (Identity and Access Management) Policy Simulator
TOEIC 準備心得
JPA Optimistic Locking With @Version
OCEJPA Study Notes
OCEJPA(1Z0-898) Preparation Tips
JPA lifecycle events practice
XDate - a modern java-script date library
How to avoid check style errors
NIG系統報表開發指南
Ease Your Effort of Putting Data into History Table
NIG 系統開發指引
NIG系統開發文件閱讀步驟
Form Bean Creation Process for NIG System
A Short Intorduction to JasperReports
Apply Template Method Pattern in Report Implementation
Utilize Commons BeansUtils to do copy object
Apply my eclipse to do entity class generation
Nig project setup quickly tutorial
Spring JDBCTemplate
Java Server Faces + Spring MVC Framework

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Insiders guide to clinical Medicine.pdf
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial diseases, their pathogenesis and prophylaxis
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Sports Quiz easy sports quiz sports quiz
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet

Spring bean mod02

  • 2. 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
  • 3. Containing your beans-2  Bean factories (defined by the org.springframework.beans.factory. BeanFactory interface) are the simplest of containers
  • 4. 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.
  • 5. Lab-Entity Class(JavaBean Class) Implements-1  規劃一個打招呼的介面interface /method
  • 7. 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
  • 12. <bean> element  <bean id=“bean識別名稱” class=“JavaBean class”>  <property name=“setter名稱” value=“屬 性值”/>
  • 13. Lab classpath 參考相對的類別庫  建立lib資料夾  匯入commons.logging-1.1.1-bin.jar/spring- beans.jar/spring-context.jar/spring- core.jar  Classpath設定
  • 14. 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
  • 15. Bean Instance  When getBean() is called, the factory will instantiate the bean and set the bean’s properties using DI.
  • 16. Spring Bean in Action-demo
  • 17. 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.
  • 18. 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.
  • 19. 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.
  • 24. 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
  • 28. 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
  • 29. 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:
  • 30. Referencing Bean Lab-1  Employee aggregation Working class  Define Working Class  Work interface
  • 31. Referencing Bean Lab-2  Define Employee class aggregation Working class
  • 32. Referencing Bean Lab-3  Spring bean config
  • 34. 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.
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. Lists and arrays  To give Hank a collection of instruments to perform with, let’s use the <list> configuration element:
  • 41. Collection Lab-1  一個員工同時間可以進行多樣工作…  規劃一個可以從事多項工作的 ChtEmployee Entity Class
  • 42. Collection Lab-2  Config配置
  • 44. 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