- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境設定
- Spring - Hello World 示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - Spring框架中的AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring ApplicationContext 容器
Application Context是Spring的高階容器。類似於BeanFactory,它可以載入bean定義,將bean連線在一起,並在請求時分配bean。此外,它還添加了更多特定於企業的功能,例如能夠從屬性檔案解析文字訊息和能夠將應用程式事件釋出給感興趣的事件監聽器。此容器由org.springframework.context.ApplicationContext介面定義。
ApplicationContext包含BeanFactory的所有功能,通常建議使用它而不是BeanFactory。BeanFactory仍然可以用於輕量級應用程式,例如移動裝置或基於applet的應用程式。
最常用的ApplicationContext實現是:
FileSystemXmlApplicationContext - 此容器從XML檔案載入bean的定義。在這裡,您需要向建構函式提供XML bean配置檔案的完整路徑。
ClassPathXmlApplicationContext - 此容器從XML檔案載入bean的定義。在這裡,您不需要提供XML檔案的完整路徑,但是您需要正確設定CLASSPATH,因為此容器將在CLASSPATH中查詢bean配置XML檔案。
WebXmlApplicationContext - 此容器從web應用程式中載入包含所有bean定義的XML檔案。
我們在Spring Hello World 示例中已經看到了關於ClassPathXmlApplicationContext容器的示例,我們將在討論基於web的Spring應用程式的單獨章節中詳細討論XmlWebApplicationContext。所以讓我們來看一個FileSystemXmlApplicationContext的例子。
示例
讓我們準備好一個可執行的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為SpringExample的專案,並在建立的專案的src資料夾下建立一個包com.tutorialspoint。 |
| 2 | 如Spring Hello World 示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。 |
| 3 | 在com.tutorialspoint包下建立Java類HelloWorld和MainApp。 |
| 4 | 在src資料夾下建立Bean配置檔案Beans.xml。 |
| 5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按如下所述執行應用程式。 |
以下是HelloWorld.java檔案的內容:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
以下是第二個檔案MainApp.java的內容:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext
("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
關於主程式,需要注意以下兩點:
第一步是建立工廠物件,我們使用框架APIFileSystemXmlApplicationContext在載入給定路徑的bean配置檔案後建立工廠bean。FileSystemXmlApplicationContext() API負責建立和初始化XML bean配置檔案中提到的所有物件,即bean。
第二步是使用建立的上下文物件的getBean()方法獲取所需的bean。此方法使用bean ID返回一個泛型物件,最終可以將其轉換為實際物件。一旦擁有了一個物件,就可以使用此物件呼叫任何類方法。
以下是bean配置檔案Beans.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 = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
建立原始檔和bean配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
Your Message : Hello World!