博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring从菜鸟到高手(二)AOP的真正实现
阅读量:7168 次
发布时间:2019-06-29

本文共 4146 字,大约阅读时间需要 13 分钟。

经过了前面一章的学习大家基本理解了SpringAOP的简单工作原理,但是那只是最基本的一些操作,Spring的设计师们知道我们不想在诸如得到一个代理类、需要实现哪些接口 这些琐碎的事情上牵扯过多的经历,毕竟我们是中国软件产业的栋梁我们还要做更重要的事情。^_^  所以他们给我们准备了好多好东西,下面我就来介绍一下
拦截器接口MethodBeforeAdvice 所在包org.springframework.aop.MethodBeforeAdvice
功能:可以在调用的目标方法之前加入功能。方法:
void ( method, [] args,  target)
拦截器接口AfterReturningAdvice  所在包org.springframework.aop.AfterReturningAdvice
功能:可以在调用的目标方法之后加入功能。方法:
void ( returnValue,  method, [] args,  target)
拦截器接口MethodInterceptor 所在包 org.aopalliance.intercept.MethodInterceptor
功能:可以在调用的目标方法前后(也可以叫周围)加入功能。方法:
 java.lang.Object ( invocation)
接口MethodInvocation 所在包 org.aopalliance.intercept.MethodInvocation
MethodInvocation 接口从父接口Joinpoint处继承到一个方法
java.lang.Object ()
调用proceed方法方法用于执行目标类的目标方法。
下面我通过现实的代码来讲解Spring中AOP的使用方法,我还使用我以前例子中的接口Foo和实现此接口的类FooClass【开发环境为Eclipse】
便于大家阅读我将全部代码贴上来,首先是接口Foo,我在这个接口中增加了一个打印整数的方法
public interface Foo {
 
public void printMessage(String message);
 public void printInteger(int num);
}
实现Foo接口的类FooClass
public class FooClass implements Foo {
 
public void printMessage(String message) {
  
  System.out.println(this.getClass().getName()+" "+message);
 }
 
public void printInteger(int num)
 {
  System.out.println(this.getClass().getName()+" "+num);
 }
}
 实现上面三个
拦截器接口【
AfterReturningAdvice,MethodBeforeAdvice,MethodInterceptor 】的类
MyAdvice
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class 
MyAdvice implements 
AfterReturningAdvice,MethodBeforeAdvice,MethodInterceptor 
{
 public void 
afterReturning(Object arg0, Method arg1, Object[] arg2,
   Object arg3) throws Throwable {
  Log log = LogFactory.getLog(MyAfterReturningAdvice.class);
  log.info("我在程序运行之后出现");
  
 }
 public void 
before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
  Log log = LogFactory.getLog(MyAfterReturningAdvice.class);
  log.info("我在程序运行之前出现");
  
 }
 public Object 
invoke(MethodInvocation arg0) throws Throwable {
  Log log = LogFactory.getLog(MyAfterReturningAdvice.class);
  log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
  
Object obj = arg0.proceed();
  log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
  
return obj;
 }
}
由于我使用的Junit测试,所以有一个Junit测试类
AOPJunitTest
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class 
AOPJunitTest extends 
TestCase {
 
ApplicationContext act = null; //使用的应用上下文类
 protected void setUp() throws Exception {
  super.setUp();
  this.act = 
new ClassPathXmlApplicationContext("/applicationContext.xml");//
读取XML配置文件
  
 }
 public void testprintMessage()
 { 
   
Foo foo = (Foo)act.getBean("proxy");//通过依赖注入得到FooClass对象的实例这个对象是Spring的一个类                                                                 org.springframework.aop.framework.ProxyFactoryBean这个类需要三个注入分别是
 void ( target) 得到注入一个目标对象(真正要干活的类)
void ([] interfaceNames) 目标对象所实现的接口数组
void ([] interceptorNames) 拦截器数组(因为我们有多种拦截器)
   foo.printMessage("Hello!");
   foo.printInteger(20);
 }
}
下面是我们的XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " ">
<beans>
 <bean id="
proxy" class="
org.springframework.aop.framework.ProxyFactoryBean">
  <property name="
target">
   <ref bean="
fooClass"/>
  </property>
  
  <property name="
proxyInterfaces">
   <value>
Foo</value>
  </property>
  
  <property name="
interceptorNames">
   <list>
    <value>
interceptorReturning</value>
   </list>
  </property>
 </bean>
 
 
 <bean id="
fooClass" class="
FooClass"></bean>
 <bean id="
interceptorReturning" class="
MyAdvice"></bean>
  
 </beans>
程序运行结果
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
INFO - 我在程序运行之前出现
FooClass Hello!
INFO - 我在程序运行之后出现
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
INFO - 我在程序运行之前出现
FooClass 20
INFO - 我在程序运行之后出现
INFO - $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
本文转自 tony_action 51CTO博客,原文链接:http://blog.51cto.com/tonyaction/42040,如需转载请自行联系原作者
你可能感兴趣的文章
DNS配置
查看>>
MPLS ××× 互访关系控制
查看>>
如果说搞技术没前途,那是因为你技术搞的还不够深
查看>>
柳州市第一职业技术学校中心机房双活虚拟引擎容灾备份系统需求
查看>>
我的友情链接
查看>>
NV 3D viosn的设置
查看>>
在CentOS系统下安装Red5
查看>>
移动端消除click事件的延迟效果
查看>>
bgp与igp交互的配置
查看>>
[官方文档] oracle官方文档总汇(9i,10g,11gR1, 11gR2)
查看>>
宝马与F团合作能否再造营销奇迹?
查看>>
10 Linux程序包管理
查看>>
Exchange2010升级SP2 (包含各角色)
查看>>
实施Exchange 2013中的分层通讯簿
查看>>
Windows下安装MySql后,出现的错误解决办法
查看>>
oracle创建只读用户
查看>>
详解mysql的tee功能 并利用其记录相关操作
查看>>
Python function
查看>>
Linux系统中程序库文件简介
查看>>
基于Linux的集群系统(四) 实现过程之理论先导篇
查看>>