1. 程式人生 > >Spring AOP基於代理的實現

Spring AOP基於代理的實現

Spring AOP是一個非常好的東西,AOP也就是面向切面程式設計。主要實現方式有三種,在這裡我們以代理的方式實現。我們以一個人睡覺為例子講解,使用的開發工具是Myeclipse。

1.首先我們需要建立一個介面,Sleepable,所有的人都可以睡覺。

package Spring_AOP_init;

public interface Sleepable {
	void sleep();
}

2.新建一個Human類來實現這個介面,人類可以睡覺

package Spring_AOP_init_impl;

import Spring_AOP_init.Sleepable;

public class Human implements Sleepable{

	public void sleep() {
		// TODO Auto-generated method stub
		System.out.println("Go to sleep,Get a good dream!");
	}
}

3.但是一個人睡覺的時候不光光只是睡覺,他還要做一些其他的動作,比如說睡覺之前要脫衣服,起來的時候要穿衣服,所以除了睡覺這個業務邏輯以外,我們需要新增些額外的功能,MethodBeforeAdvice,AfterReturningAdvice這兩個介面可以就是增強介面,前置和後置。

package Spring_AOP_init_impl;

import java.lang.reflect.Method;

import org.springframework.aop.*;

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{

	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("We need dress out before sleep!");
	}

	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
			Object arg3) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("We need dress in cloth after sleep!");
	}
}
4.之後就是最重要的配置配置檔案了
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<!-- 被代理物件 -->
	<bean id="human" class="Spring_AOP_init_impl.Human"></bean>
	
	 <!-- 定義通知內容,就是切入點執行前後需要做的業務 -->
	<bean id="sleephelper" class="Spring_AOP_init_impl.SleepHelper"></bean>
	
	<!-- 定義切入點位置 -->  
    <bean id="sleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">  
        <property name="pattern" value=".*sleep"></property>  
    </bean> 
    
    <!-- 使切點與通知關聯起來,完成切面的設定 -->
    <bean id="sleephelperadvisor"  class="org.springframework.aop.support.DefaultPointcutAdvisor">
    	<property name="advice" ref="sleephelper"></property>
    	<property name="pointcut" ref="sleepPointcut"></property>
    </bean>
    
    <!-- 設定代理 -->
    <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    	<!-- 代理的物件 -->
    	<property name="target" ref="human"></property>
    	<!-- 使用切面 -->
    	<property name="interceptorNames" value="sleephelperadvisor"></property>
    	<!-- 代理介面,代替睡覺 -->
    	<property name="proxyInterfaces" value="Spring_AOP_init.Sleepable"></property>
    </bean>
</beans>
5.配置完成以後也就將這個代理的過程弄好了,之後就是測試了,新建一個測試類。
package org.etspace.test;

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

import Spring_AOP_init.Sleepable;

public class Test_AOP_Proxy {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ac=new FileSystemXmlApplicationContext("src/applicationContext.xml");
		Sleepable human=(Sleepable)ac.getBean("proxy");
		human.sleep();
	}
}

進行測試

這就是代理實現AOP的全部過程啦,還有其他的兩種方式正在學習中~