这几天一直没有时间写博客,今天正好抽空,现在继续上一篇文章,来整理一下个人心得,希望可以给初学者帮助,即使没有帮助,也希望大家共同交流。
下面开始进入正题,这篇文章主要讲解的是有关于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方法,否则会报错。