這幾天一直沒有時間寫博客,今天正好抽空,現在繼續上一篇文章,來整理一下個人心得,希望可以給初學者幫助,即使沒有幫助,也希望大家共同交流。
下面開始進入正題,這篇文章主要講解的是有關於Spring AOP中的Advices的實現,首先介紹的是通過接口實現的advice,其餘實現將在後面幾篇文章中介紹。
1、定義基本的advice Bean以及要被調用的類
定義Advice Bean
package com.javaeye.sunjiesh.aop;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
public class AdviceTest implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor,ThrowsAdvice{
/**
* 調用方法之前執行
* Before Advice
*/
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("調用before");
}
/**
* 調用方法之後執行
* After Advice
*/
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("調用afterReturning");
}
/**
* Around Advice
*/
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result=null;
System.out.println("around 調用before之前");
result=arg0.proceed();
System.out.println("around 調用afterReturning之後");
return result;
}
public void afterThrowing(Method method,Object[] args,Object target,Throwable subclass) {
System.out.println("ThrowAdvice 記錄異常...........");
}
}
接口類
package com.javaeye.sunjiesh.proxy;
public interface IHello {
void hello();
void helloA(String a);
void helloB(String b);
}
實現類
package com.javaeye.sunjiesh.proxy;
public class HelloImpl implements IHello {
@Override
public void hello() {
// TODO Auto-generated method stub
System.out.println("hello in HelloImpl");
}
@Override
public void helloA(String a) {
// TODO Auto-generated method stub
System.out.println("helloA in HelloImpl,parameter is "+a);
}
@Override
public void helloB(String b) {
// TODO Auto-generated method stub
System.out.println("helloB in HelloImpl,parameter is "+b);
}
}
2、在配置文件中聲明我們的bean(文件名:spring-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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 代理 --> <bean id="helloImpl" class="com.javaeye.sunjiesh.proxy.HelloImpl"></bean> <!-- 聲明四種通知類型,其實就是你想加入到其他被代理程序執行邏輯中的代碼 --> <bean id="adviceTest" class="com.javaeye.sunjiesh.aop.AdviceTest" /> <!-- 基礎的通知使用,這裏強行給helloSpeaker1加上了advice通知 --> <bean id="aaa" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="helloImpl"></property> <property name="interceptorNames"> <list> <value>adviceTest</value> </list> </property> </bean> </beans>
3、在main函數中調用
package com.javaeye.sunjiesh.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javaeye.sunjiesh.proxy.IHello;
public class AopMain {
/** 對於給基本的spring bean強行指定一批advice方法的調用展示 **/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-beans.xml");
IHello h = (IHello) ctx.getBean("aaa");
h.helloA("測試一");
System.out.println("======開始進行測試二=========");
try {
h.helloB("測試二");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、控制檯的顯示
around 調用before之前 調用before helloA in HelloImpl,parameter is 測試一 調用afterReturning around 調用afterReturning之後 ======開始進行測試二========= around 調用before之前 調用before helloB in HelloImpl,parameter is 測試二 調用afterReturning around 調用afterReturning之後
備註:如果Advice Bean實現ThrowsAdvice接口,則一定要實現afterThrowing方法,否則會報錯。