首先定義幾個關鍵性的概念:
Beans:在Spring中,組成程式主體的并由Spring IoC容器管理的對象,稱為Beans。Beans以及任何與之存在依賴關系的物件,均被反射在由容器使用的Configuration Metadata中。
IoC容器的核心
org.springframework.beans和org.springframework.context這兩個包構成了Spring架構的IoC容器的核心。其中BeanFactory接口提供了能管理任何對象的進階配置機制。但是目前一般不直接使用BeanFactory類,而是用ApplicationContext替代之。ApplicationContext是BeanFactory接口的派生,可以看作是BeanFactory的超集。它能夠更容易的與Spring AOP特性內建,擁有消息資源處理(Message Resources Handling)和事件釋出(Event Publication)等功能。
<b> </b>ApplicationContext接口代表了Spring IoC容器。主要負責初始化,配置,組裝Beans。這個容器從Configuration Metadata中得到相關的指令。Configuration Metadata一般是一個XML,JAVA注解,或者JAVA代碼。
下面是一個典型的XML配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<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="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>
ApplicationContext接口主要有兩種實作類,分别是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext。從命名上很容易看出它們的适用範圍。
初始化一個ApplicationContext是極其容易的事情。下面是一般的初始化方式:
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
從以上代碼可以很容易看出,應用程式可以使用多個配置檔案,這樣做的好處是顯而易見的。另外,還可以在某一個配置檔案中引入其它的配置檔案,如下所示:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
Beans還可以通過靜态工廠方法來初始化,例如:類的定義代碼是:
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
在配置檔案中定義為:
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
Beans
在容器内部,Beans由BeanDefinition對象表示。BeanDefinition對象包含了以下一些資訊:
-完整的類名稱,包含包的路徑。
-Bean行為的相關元素,說明了Bean在容器内的行為。
-Bean相關的引用,也就是依賴的其它的類。
-Bean其它的設定資訊。
依賴注入(Dependency Injection)
Spring中DI主要有兩種形式:基于構造方法的和基于Setter方法的。
對于基于構造方法的,例如下面這個類:
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
// ...
則配置檔案應做如下配置:
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
對于參數是Bean的時候,這種方法是可以的。但是如果參數裡面含有基本資料類型,比如int或者String,那麼Spring便無法确定如何解析。必須采用以下的方法對待:
package examples;
public class ExampleBean {
// No. of years to the calculate the Ultimate Answer
private int years;
// The Answer to Life, the Universe, and Everything
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
在配置檔案中配置:
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
或者
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
對于基于Setter方法的:
例如下面這個類:
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
public void setIntegerProperty(int i) {
this.i = i;
則在配置檔案中應做如下配置:
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<!-- setter injection using the neater 'ref' attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
以上例子均摘自Spring官方參考文檔。
附Spring 3架構結構圖
本文轉自 kevx 51CTO部落格,原文連結:http://blog.51cto.com/spinlock/323821,如需轉載請自行聯系原作者