Spring bean mod02


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Spring bean mod02

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