Sunday, April 20, 2014

[Spring] 2 - Spring Quick Start

TOPIC
  • Application Classes Example with Configuration Instruction XML file
  • Compare with Constructor DI vs. Setter DI
  • Bean definition examples for Constructor DI and Setter DI
  • Bean definition examples for Setter DI with
    • values
    • static Factory Methods  
    • instance Factory Methods
  • What is BeanFactory?
  • What is application context?
  • Create application context with multiple files
  • Bootstrapped in any environment


Spring quick start


  • Spring manages the lifecycle of the applicaiton
    • All beans are fully initialized before they are used ???
  • Beans are always created in the right order 
    • Based on the dependencies
    • [Nattily] with <order>
  • Each bean is bound to a unique id
    • The id reflects the service the bean provides to clients
  • Spring encapsulates (~includes outside cannot see the details inside) the bean implementations chosen for a given application deployment.
    • Conceals(~hide) implementation details

Writing bean definitions
  • [Nattily] Write a bean configuration xml file can convert into Java code with unique bean id.
  • <bean> element
    • The root of the .xml file
    • The root element of any Spring configuration file
  • <bean> element
    • Tells the Spring container about the class and how it should be configured.
  • id attribute
    • takes the unique id
  • class attribute 
    • specifies the bean's fully qualified class name
  • e.g. <bean id='glt0001' class ="com.hsbc.beans.SimpleBean">
  • <property> element
    • Tells the Spring container to call setName(), setStaffId() while setting the "name" and "staffId" property.
  • <value> element 
    • Defines the value of "name" and "staffId" as "xyz" and "10551328"
  • The container instantiates the 'SimpleBean' based on the XML definition as:
  • Result:
    • SimpleBean glt001 = new SimpleBean();
    • glt001.setName("xyz");
    • glt001.setStaffId("10551328");

Types of Dependency Injection shown in coding

As discussed in last chapter, there are 2 types of Dependency Injection.

(1) Constructor Dependency Injection

  • Enforce mandatory dependencies
  • Promote immutability (cannot be changed forever)
  • Concise(~clear and simple) for programmatic usage
(2) Setter Dependency Injection
  • Have descriptive names
  • Follow JavaBean conventions
  • Inherited automatically

When to Use Constructors DI vs Setter DI?
Spring supports both
You can mix and match


General Recommendations
  • Follow standard Java Design guidelines
    • Use constructors to set required properties
    • Use setters for optional or those was default values 
  • You may find some classes are designed for a particular injection strategy
  • Be consistent above all

Combining Constructor and Setter Dependency Injection



Injecting Scalar Values


Automatic Value Type Conversion 


Calling Static Factory Methods
Q: A class can have both constructor and static method at the same time?

Calling Instance Factory Methods


Creating an application context
  • Spring application contexts can be bootstrapped in any environment, including
    • JUnit system test
    • Web application
    • Enterprise Java Bean (EJB)
    • Standalone application
  • Loadable with bean definitions from files:
    • In the class path
    • On the local file system
    • At an environment-relative resource path
  • [Nattily] 2 most fundamental packages:
    • org.springframework.beans packages
    • org.springframework.context packages
  • [Nattily] The ApplicationContext builds on top of the BeanFactory(it's a subclass).
  • [Nattily] The BeanFactory provides the configuration framework and basic functionality, while ApplicationContext added enhanced capabilities to it, some of them perhaps more J2EE and enterprise centric.

[Nattily] What is BeanFactory? [link]
  • The BeanFactory is the actual container which instantiates, configures, and manages a number of beans.
  • A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, for which there are multiple implementations. 
  • the BeanFactory does have to be instantiated somehow.
  • Resource res = new FileSystemResource("beans.xml");
    XmlBeanFactory factory = new XmlBeanFactory(res);
  •  

[Nattily] What is Application Context? [link]
  • ApplicationContext提供取得資源檔案更方便的方法。
  • ApplicationContext提供文字訊息解析的方法,並支援國際化(Internationalization, I18N)訊息。
  • ApplicationContext可以發佈事件,對事件感興趣的Bean可以接收到這些事件。

Create a Spring Application Context from Multiple Files
  • A context can be configured from multiple files
  • Allows partitioning of bean definitions into logical groups
  • Generally a best practice to separate out "application" beans from "infrastructure" beans
    • Infrastructure often changes between environments
    • Evolves (逐漸演變) at a different rate





Bootstrapping in Each Environment


No comments:

Post a Comment