Spring AOP之增強(aspectj)

按照增加在目標類方法連接點的位置可以將增強劃分爲以下五類:
  • 前置增強   (org.springframework.aop.BeforeAdvice)   表示在目標方法執行前來實施增強
  • 後置增強   (org.springframework.aop.AfterReturningAdvice)   表示在目標方法執行後來實施增強
  • 環繞增強   (org.aopalliance.intercept.MethodInterceptor)   表示在目標方法執行前後同時實施增強
  • 異常拋出增強   (org.springframework.aop.ThrowsAdvice)   表示在目標方法拋出異常後來實施增強
  • 引介增強   (org.springframework.aop.introductioninterceptor)   表示在目標類中添加一些新的方法和屬性
其中,引介增強是一種特殊的增強。他可以在目標中添加屬性和方法,通過攔截定義一個接口,讓目標代理實現這個接口。他的連接點是級別的,而前面的幾種則是方法級別的。

其中,環繞增強是AOP聯盟定義的接口,其他四種增強接口則是Spring定義的接口

package Before;

public interface Waiter {
	public void greetTo(String name);
	public void serveTo(String name);
}

package Before;

public class NativeWaiter implements Waiter {
	@Override
	public void greetTo(String name) {
		System.out.println("greet to " + name + "...");
	}
	@Override
	public void serveTo(String name) {
		System.out.println("serving to " + name + "...");
	}
	
}
package Before;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class GreetingBeforeAdvice implements MethodBeforeAdvice{
	@Override
	public void before(Method method,Object[] args,Object obj) {
		String clientName = (String)args[0];
		System.out.println("How are you!Mr." + clientName);
	}
}
<?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"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <context:annotation-config />
    
    <bean id="advice" class="Before.GreetingBeforeAdvice"></bean>
    <bean id="target" class="Before.NativeWaiter"></bean>
    <bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
    	p:proxyInterfaces="Before.Waiter"
    	p:target-ref="target"
    	p:interceptorNames="advice"
    	 />

</beans>


package Before;

import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAdvice {

	public static void main(String[] args) {
		//通過代碼實現aop aspectj
//		Waiter target = new NativeWaiter();
//		BeforeAdvice advice = new GreetingBeforeAdvice();
//		
//		ProxyFactory pf = new ProxyFactory();
//		pf.setTarget(target);
//		pf.addAdvice(advice);
//		
//		Waiter proxy = (Waiter)pf.getProxy();
//		proxy.greetTo("aaaa");
//		proxy.serveTo("bbbb");
//		
		
		//通過xml
		ApplicationContext ctx = new ClassPathXmlApplicationContext("conf.xml");
		Waiter waiter = (Waiter)ctx.getBean("waiter");
		waiter.greetTo("ccc");
		waiter.serveTo("dddd");
		
	}

}
注意需要使用AspectJ需要引入一些jar包:

aopalliance.jar

aspectjrt.jar

aspectjtools.jar

aspectjweaver.jar

org.aspectj.matcher.jar

可以根據需要引入上面的部分包,不一定全部需要引入,然後就可以編譯通過了,

如果還缺其他的包,可以百度查,然後下載下來就可以了。

資源下載地址:http://download.csdn.net/download/a1317338022/10130277










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章