顯示具有 spring framework 標籤的文章。 顯示所有文章
顯示具有 spring framework 標籤的文章。 顯示所有文章

Spring Bean Scope 學習

在Spring中定義一個Bean時,可以針對其scope加以設定。在新版的Spring中,共有五種不同的scope可以設定,分別為:

Spring Bean Definition

在Spring中,Bean是一個物件,藉由Spring IoC Container來初始化(instantiated)、管理組裝(assembled)。

Bean會由定義的設定檔(configuration metadata)來建立。例如,藉由在XML檔案中,<bean>的設定來建立。

設定檔(configuration metadata)需要瞭解下列事項:

  • 如何建立Bean
  • Bean的生命週期
  • Bean的相依關係(dependencies)

在一個Bean的設定檔中,透過下列的properties來進行設定:

  • class:必定存在。定義用來建立此顆bean的class
  • name:用來建立Bean的識別(identifier)
  • scope:定義Bean的Scope,包含:singleton、protopype、request、session和global-session
  • constructor-arg:用來注入dependencies
  • properties:用來注入dependencies
  • autowiring mode:用來注入dependencies
  • lazy-initialization mode:用來告知IoC Container在第一次request這個bean的時候再建立instance,而不是在startup時
  • initialization method:一個callback。在所有必要的properties被呼叫後執行
  • destructor method:一個callback。在bean被destroyed時後被呼叫

一個典型的Bean設定檔大致如下所示:

在該設定檔中,有一個HelloWorld的Bean,分別在初始和結束時,會去呼叫init和destroy方法。同時把message這個參數注入Hello World!的值。

Spring Social 介紹

Spring Social 是最近Spring Project中所提供的一個Framework,它讓使用者可以把你原有的Spring Application和Thrid-Party Service Provide(ex : facebook, twitter)做整合,讓你用這些SaaS來認證,操作其API...等等。