前陣子我在驗證 Spring 3.0 的 AOP 功能,動手寫程式之前,得先把開發環境搭起來。最直覺的做法是上 Spring 官網把相關的 jar 包抓下來,可實作起來才發現,這些 jar 包彼此之間還有千絲萬縷的依賴關係,缺一個就得再手動補一個。折騰下來環境雖然能跑,卻要為了「又缺哪個包」反覆搜尋、下載、丟進 classpath,效率實在算不上好。
其實有個更省事的路子:把整個專案交給 Maven 管理,讓它依據設定檔自動解析並下載對應版本的 jar 包。以下就是我當時的具體做法。
先到官方網站取得 Maven 的安裝包,我當時用的是 3.0.1 版本。解壓、設定環境變數這些基本步驟這裡就不展開了。
想在 Eclipse 裡靠 Maven 建構專案,前提是 IDE 中已經正確裝好 Maven 插件,這一步不能跳過。
在專案根目錄新增一個名為 pom.xml 的檔案,把下面的內容貼進去並存檔,Eclipse 便會自動把缺少的 jar 包拉下來。其中 <dependencies> 區塊就是宣告依賴的地方。
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TRSEKP</groupId>
<artifactId>TRSEKP-V6.6</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TRSEKP-V6.6</name>
<description>TRSEKP V6.6 測試專案</description>
<properties>
<project.build.sourceEncoding>GBK</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入 Spring-AOP 等相關 Jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>
</project>
接著在 Eclipse 中新增一個類別,例如 com.trs.components.mgr,程式碼如下:
package com.trs.components.mgr;
import com.trs.components.persistent.Student;
public class StudentMgr implements IStudentMgr {
public Student saveOne(String _sName) throws Exception {
System.out.println("保存了一個學生對象..");
return null;
}
public void saveMany(String _sName) throws Exception {
System.out.println("保存了多個學生對象..");
}
}
在原始碼目錄下建立 applicationContext.xml,Spring 的 bean 定義都寫在這裡:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop"
xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="StudentMgr" class="com.trs.components.mgr.StudentMgr" />
</beans>
配置寫完後,再開一個測試類別。只要用下面的程式碼,就能從容器中取出 StudentMgr 的實例並呼叫它的方法:
// 使用 ApplicationContext 來初始化系統
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 透過 Spring 取得實例物件
StudentMgr studentMgr = (StudentMgr) context.getBean("StudentMgr");
System.out.println("-----------");
studentMgr.saveMany("wuguowei");
能正常取得物件並印出訊息,就代表 Maven 依賴管理與 Spring 容器都已經就緒,接下來便能專心往 AOP 的實驗推進。