Spring Hello World Example 專案 之 AOP Before

(此圖來源Spring官網改之)


本範例實作說明:
本篇主要使用 Spring Framework AOP (Aspect Oriented Programming)
,做一個基本測試專案,
本例有使用到 Spring Bean 、 ProxyFactoryBean 、MethodBeforeAdvice。
用簡單的程式,來實作。
功能需求: 在顯示 Hello World 的方法前 加入 呼叫方法的訊息。

一、範例開發準備工具
 
作業系統:Windows 7
開發工具:Eclipse Java EE IDE for Web Developers Juno Service Release2
JAVA JDK : JDK 1.6
其它相關:Maven 4.0
 Springframework 3.2.2.RELEASE

二、新增Maven範例專案
範例專案名稱: SpringHelloWorldExampleAOP


圖1 開啟Eclipse -> File -> New -> Maven Project
   (如果找不到Maven Project ,請選Other..-> Maven -> Maven Project)


圖2 選擇專案存放位置 use default Workspase location-> Next
   選擇存放在預設的工作資料夾下(就是你開Eclipse時,選擇Workspase下)


圖3 使用 Maven 範本
   本例選用:
Group id : org.apache.maven.archetypes
Artifact id : maven-archetype-quickstart
Version : 1.1
   -> Next
註:
Group id為公司名稱 or 群組分類名稱
Artifact Id 為專案標識符,就是專案名稱



圖4 範例專案
Group id : SpringHelloWorldExampleAOP
Artifact id : SpringHelloWorldExampleAOP
Version : 0.0.1-SNAPSHOT
Package : com.levin
     
-> Finish


圖5 新增專案後,基本預設資料


三、使用相關jar檔


主要:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>


圖6 開啟 pom.xml

 

pom.xml內容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>SpringHelloWorldExample</groupId>
<artifactId>SpringHelloWorldExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SpringHelloWorldExample</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.2.RELEASE</spring.version>
<jdk.version>1.6</jdk.version>
</properties>

<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

<build>
<finalName>SpringHelloWorldExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>



四、相關resources
 
建立resources 路徑
src/main/resources

圖7 建立相關 resources



圖8 將 resources 加入到 source folders on build path。

在專案按右鍵 -> Properties

進入Properties介面 -> Java Build Path
-> Source -> Add Folder..
->進入 Source Folder Selection介面
-> 選擇 打勾 resources
   -> OK -> OK


五、建立 Spring bean檔 SpringBeans.xml
src/main/resources/SpringBeans.xml
圖9

 

主要內容:
   <bean id="helloBean" class="com.levin.HelloWorld">
<property name="name" value="mySpring"/>
</bean>



說明:
id="helloBean"
為bean命名設定,此bean名稱為helloBean。

class="com.levin.HelloWorld"
為實體檔位置。

<property name="name" value="mySpring"/>
為 參數設定,參數為name,值為mySpring
       (此name 對應到HelloWorld.java 裡的name。)

 

AOP 設定:
   使用 ProxyFactoryBean 
<bean id="aopTest" class="org.springframework.aop.framework.ProxyFactoryBean">
設定Interfaces
<property name="proxyInterfaces" value="com.levin.IHelloWorld"/>
設定執行對像
<property name="targetName" value="helloBean"/>
設定攔截器
<property name="interceptorNames" >
<list><value>helloBeforeAdvice</value></list>
</property>
</bean>
SpringBeans.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="helloBean" class="com.levin.HelloWorld">
<property name="name" value="mySpring"/>
</bean>

<bean id="helloBeforeAdvice" class="com.levin.HelloBeforeAdvice"/>

<bean id="aopTest" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.levin.IHelloWorld"/>
<property name="targetName" value="helloBean"/>
<property name="interceptorNames" >
<list><value>helloBeforeAdvice</value></list>
</property>
</bean>


</beans>



六、建立 Java 檔
建立四支Java 檔
路徑檔名:src/main/java/com/levin/HelloWorld.java
路徑檔名:src/main/java/com/levin/MyMain.java
路徑檔名:src/main/java/com/levin/HelloBeforeAdvice.java
路徑檔名:src/main/java/com/levin/IHelloWorld.java

圖10


七、編輯 IHelloWorld.java
路徑檔名:src/main/java/com/levin/IHelloWorld.java

IHelloWorld.java 程式內容:
package com.levin;

public interface IHelloWorld {
public void printHello();
}




八、編輯 HelloWorld.java
路徑檔名:src/main/java/com/levin/HelloWorld.java


HelloWorld.java 程式內容:
package com.levin;

/**
* Spring HelloWorld bean
*
*/
public class HelloWorld implements IHelloWorld {

private String name;

public void setName(String name) {
this.name = name;
}

public void printHello() {
System.out.println("Spring 3 : Hello ! " + name);
}
}

注意事項:
使用 Spring ,變數需要有setXxx()的方法。
才能在xml中的property對應中給值。
set方法使用:
變數名前面加 set
set + 變數名(第一字大寫)
例:

private String myName;
public void setMyName(String name) {
this.myName = name;
}




九、編輯 HelloBeforeAdvice.java
路徑檔名:src/main/java/com/levin/HelloBeforeAdvice.java
說明:是在執行對來的方法前來before這方法

HelloBeforeAdvice.java 程式內容:
package com.levin;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

/**
* Spring aop HelloWorld
*
*/
public class HelloBeforeAdvice implements MethodBeforeAdvice {

public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("AOP Test Method before :" + method);
}
}


注意事項:
記得implements MethodBeforeAdvice 加入 before 的方法。



十、編輯 MyMain.java
路徑檔名:src/main/java/com/levin/MyMain.java

MyMain.java 程式內容:
package com.levin;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyMain {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml");

IHelloWorld obj = (IHelloWorld) context.getBean("aopTest");
obj.printHello();
}

}




十一、專案所有的檔
共五支檔,四支java程式,一支xml檔
HelloBeforeAdvice.java
HelloWorld.java
IHelloWorld.java
MyMain.java
SpringBeans.xml
圖11


十二、測試 - 執行
圖12 在專案按右鍵 -> Run As -> Java Application


圖13 執行結果



十三、其它參考
Struts + Spring + Hibernate 目錄


Spring Framework 首頁

Spring Framework API 2.0 Interface MethodBeforeAdvice

 
















 








其它文章

arrow
arrow
    文章標籤
    springframework AOP Spring bean
    全站熱搜

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()