2017年12月20日 星期三

Java Spring - Application context及Bean生命週期


  • Spring致力於簡化企業級的Java開發,low coupling,關鍵在於DI及AOP
  • Bean
    • Spring container負責create對象,wired,configured並管理他們的生命週期(new()~finalize())
    • Container是Spring framwork的核心,使用DI管理組件,因此會簡單乾淨,容易理解。更重要的是,容易進行unit test
    • Spring container可歸為兩種不同類型:
      • bean factory: (org.springframework.beans.factory.BeanFactory interface)提供基本DI support,對大多數應用來說太low-level
      • application context: (org.springframework.context.ApplicationContext interface)基於bean factory建置,提供application框架級的服務
  • 使用application context
    • AnnotationConfigApplicationContext
      • 基於Java-based configuration classes 載入application context 
    • AnnotationConfigWebApplicationContext
      • 基於Java-based configuration classes 載入web application context
    • ClassPathXmlApplicationContext
      • 從classpath下的一的或多個XML files載入context,並把這些定義文件作為 class- path resources 
    • FileSystemXmlApplicationContext
      • 從filesystem的一的或多個XML files載入context
    • XmlWebApplicationContext
      • 從web application下的一的或多個XML configured files載入context
    • 無論是從ClassPath或FileSystem載入context,將bean load至bean factory過程都類似
      • FileSystemXmlApplicationContext (在 filesystem 指定的路徑找xml文件)
    • ApplicationContext context = new FileSystemXmlApplicationContext("c:/knight.xml"); 
      
      • ClassPathXmlApplicationContext (在所有的classpath內含Jar,找xml文件)
    • ApplicationContext context = new
                      ClassPathXmlApplicationContext("knight.xml");
      
      • AnnotationConfigApplicationContext(在Java config中load application context,不是XML)
    • ApplicationContext context = new AnnotationConfigApplicationContext(
          com.springinaction.knights.config.KnightConfig.class);
      
    • 接著就可以使用getBean()從Spring container中得到bean
  • Bean的生命週期
    • 傳統Java bean 能夠使用new()產生,不再被使用時,Java自動進行GC
    • Spring container的bean生命週期較複雜
    • from spring in action book
    • 步驟:
      1. Spring instantiates bean
      2. Spring injects value及bean reference 到對應的bean的屬性中
      3. 如果bean implements BeanNameAware,Spring將bean的id傳給
      4. setBeanName() 
      5. 如果bean implements BeanFactoryAware,Spring將呼叫setBeanFactory(),將BeanFacory container instance傳入
      6. 如果bean implements ApplicationContextAware,Spring將呼叫setApplicationContext(),並將bean所在的application context reference傳入
      7. 如果bean implements BeanPostProcessor,Spring將呼叫postProcessBeforeInitialization() 
      8. 如果bean implements InitializingBean Spring將呼叫afterPropertiesSet()。同樣的,如果bean使用init method宣告,那麽該初始化方法也會被用到
      9. 如果bean implements BeanPostProcessor,Spring將呼叫他的postProcess- AfterInitialization()
      10. 此時bean已經可以被應用程式使用而且會存在於此application context直到application context被消毀
      11. 如果bean實作DisposableBean,Spring將會呼叫自己的destroy(),且如果有宣告自己的destroy method,這個method就會被使用
  • Spring landscape
    • 在Spring framwork外還存在一種在core framework上的 ecosystem,可將Spring擴展到不同領域,如NoSQL、REST等
    • Spring modules
      • Spring framework由20個不同的modules組成
      • 可自由選擇適合的modules
      • Spring也提供和第三方framework和class的介接
    • Spring modules:
      • Core Spring container
        • 最核心的部分,管理Spring中bean的create, configure, management。除了bean factory, application context,module也提供很多企業服務如E-mail, JNDI, EJB, ...
        • 當configure application時,其實也是在使用至些class
      • AOP module
        • AOP support,能夠將application系統的concerns decouple
      • Data access and declaration
        • Spring JDBC及DAO(Data Access Object) module抽象這些 boilerplate code,使連結DB的code變得簡單明瞭,還可以避免關閉DB失敗導致其他問題
        • 亦提供ORM(Object-Relational Mapping) module
        • 對許多ORM framework support如Hibernate, Java Data Object, ..
        • 包含Java Message Service之上建構的Spring abstract layer
        • 使用Spring AOP module為Spring中的對象提供transaction management service
      • Web and remoting 
        • Spring MVC
        • Spring remoting集合了RMI(Remote Method Invocation)的framework,且還有Spring自己的HTTP invoker,對REST API也有support
      • Instrumentation
        • 為JVM增加agent功能,能夠為Tomcat傳倜class-type的文件
      • Test
        • 為使用JNDI, Servlet, Portlet寫unit test提供了一系列mock的實現
    • Spring portfolio 其他組合
      • Spring Web Flow
        • 類似於購物車等等的會話式流程提供support
      • Spring Web Service
      • Spring Security
      • Spring Intergration
      • Spring Batch
        • 對data進行大量操作
      • Spring Data
        • 對使用任何DB都變得非常容易
        • 也支援NoSQL
      • Spring Social
      • Spring Mobile
      • Spring for Android
      • Spring Boot
        • 簡化眾多的coding,消除很多boilerplate code
        • 以Spring的視角致力於簡化Spring
        • 依賴自動配置

沒有留言:

張貼留言