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設定檔大致如下所示:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
<bean id="helloWorld" class="HelloWorld" destroy-method="destroy" init-method="init"> | |
<property name="message" value="Hello World!"/> | |
</bean> | |
</beans> |
0 意見:
張貼留言