Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下.

  假设我们现在需要在针对数据库进行CRUD操作时添加一组日志,即在执行CRUD方法前后分别加上一句话,实现简单的面向切面编程的功能.我用到的是spring4,在配置文件上较之之前的版本可能有些不同.

  使用springAPI来实现AOP,除了spring必不可少的核心jar包,还需要两个jar包需要导入:

    1.   aspectjweaver.jar   下载链接: http://download.csdn.net/detail/luojiming1990/5432831
    2.   aopalliance.jar    下载链接:http://download.csdn.net/detail/zhaoshe/3153090

  并且配置文件的头文件也需要略作修改,需要加入aop的命名空间(namespace),详见下面实例中的beans.xml.

  UserService类(省略数据库操作代码,只做简单的打印来模拟):

public class UserService {

    public void add(){
System.out.println("添加用户");
}
public void delete(){
System.out.println("删除用户");
}
public void update(){
System.out.println("修改用户");
}
public void search(){
System.out.println("查询用户");
}
}

Log类(在执行增删改查方法前执行):

public class Log  implements MethodBeforeAdvice{

    /***
* method:被调用的方法对象
* arg1:被调用的方法的参数
* target:被调用方法的目标对象
*/
@Override
public void before(Method method, Object[] arg1, Object target)
throws Throwable {
System.out.println(target.getClass().getName()+"中的"+method.getName()+"方法被执行");
} }

AfterLog类(在执行增删改查方法后执行):

public class AfterLog implements AfterReturningAdvice{
/**
* returnValue:返回值类型
* method:被调用的方法对象
* arg1:被调用的方法的参数
* target:被调用方法的目标对象
*/
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"中的"+method.getName()+"方法被执行成功,返回值是"+returnValue); }

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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.wang.service.UserService"></bean>
<bean id="log" class="com.wang.log.Log"></bean>
<bean id="afterlog" class="com.wang.log.AfterLog"></bean>
<aop:config>
<!--"*"为通配符表示所有方法,第一个* 表示任意返回值 第二个*表示所有方法
".."表示任意个数的参数 -->
<aop:pointcut expression="execution(* com.wang.service.UserService.*())" id="pointcut"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
</beans>

测试代码testDemo:

   @Test
public void test1(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)context.getBean("userService");
userService.add();
userService.search();
}

运行测试可以看到控制台的打印结果:

com.wang.service.UserService中的add方法被执行
添加用户
com.wang.service.UserService中的add方法被执行成功,返回值是null

com.wang.service.UserService中的search方法被执行
查询用户
com.wang.service.UserService中的search方法被执行成功,返回值是null

在配置文件中,我们看到了一些这样的代码:

 <aop:config>
<!--"*"为通配符表示所有方法
".."表示任意个数的参数 -->
<aop:pointcut expression="execution(* com.wang.service.UserService.*())" id="pointcut"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>

我们来介绍几个AOP的相关概念,这个就比较好理解了:

  • 切面(Aspect):在本例中.add(),delete(),等方法中都有一些代码,在真实的程序中这里不会只是简单的打印语句而是一些有意义的代码,这些代码可以看做是AOP的切面.
  • 通知(Advisor):本例中的两个日志类,这里可以成为拦截器,都是实现了某个*Advisor接口,这两个类就是指AOP中的通知,一旦Spring符合了条件,就会发出通知,与生活中我们所说的通知不同的是,Spring中的通知是带有执行代码的,能实现某种功能.
  • 切入点(pointcut):在配置拦截器(<aop-config>)的时候,xml中配置了UserService中所有的方法都是用拦截器,这个配置是通过spring中的一个已经写好的类完成的,这个类能配置对哪些方法使用拦截器,从那个地方"切入"进去.配置是可以使用通配符.

简而言之:"切入点"负责往"什么地方"插入代码,"通知"负责插入"什么代码".

  SpringAOP将公共的业务(如日志,安全)和领域业务结合,当执行领域业务时候把公共业务加进来,实现公共业务的重复利用,使得领域业务功能更加纯粹,程序员可以专注于领域业务.

  当然除了使用springAPI,我们也可以通过自定义的类,即不需要实现任何借口或继承任何类,的方式来实现上述功能:

  只需要一个Log类:

public class Log  {

    public void before(){
System.out.println("执行方法前");
} public void after(){
System.out.println("执行方法后");
}
}

修改配置文件beans.xml中的<aop-config>为:

    <aop:config>
<aop:aspect ref="log">
<aop:pointcut expression="execution(* com.wang.service.UserService.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>

执行上面的测试代码,打印出的结果是:

执行方法前
添加用户
执行方法后
执行方法前
查询用户
执行方法后

其实这种配置方式还可以用注解来完成,下面介绍一下使用注解的方式,顺带讲一个环绕方法,它和before和after一样,不过是在某一个方法前后都会执行的代码:

  Log类:

@Aspect
public class Log {
@Before("execution(* com.wang.service.*.*(..))")
public void before(){
System.out.println("执行方法前");
}
@After("execution(* com.wang.service.*.*(..))")
public void after(){
System.out.println("执行方法后");
} @Around("execution(* com.wang.service.*.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕前");
System.out.println("签名:"+pjp.getSignature());
//执行目标方法
Object proceed = pjp.proceed();
System.out.println("环绕后");
return proceed;
}

在beans.xml中,只需要将<aop-config>修改为一行代码:

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.wang.service.UserService"></bean>
<bean id="log" class="com.wang.log.Log"></bean>
<aop:aspectj-autoproxy/>
</beans>

打印结果如下,读者自行理解,这里对于around不再解释:

环绕前
签名:void com.wang.service.UserService.add()
执行方法前
添加用户
环绕后
执行方法后

如果希望在before和after方法中得到当前执行方法的方法名或者参数的话,可以在bofore或者after方法中加一个参数,就是上面around方法中的参数,

  获得方法名:String methodName=pjp.getSignature().getName();

  获得方法参数列表:List<Object> list=Arrays.toList(pjp.getArgs());

Spring支持5种类型的通知注解:

  @Before:前置通知,在方法执行前执行

  @After:后置通知,在方法执行后执行(此注解标注的方法无法得到方法的返回值)

  @AfterRunning:返回通知,在方法返回结果之后执行(此注解标注的方法,可以获取到方法的返回值)

  @AfterThrowing:异常通知,在方法抛出异常后执行

  @Around:环绕通知,问绕着方法执行(环绕通知必须要有ProceedingJoinPoint类型的参数,且必须要有返回值,见上面).

(一)使用springAPI以及自定义类 实现AOP-aop编程的更多相关文章

  1. spring笔记--使用springAPI以及自定义类 实现AOP的一个例子

    Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...

  2. [Yii2.0] 以Yii 2.0风格加载自定义类或命名空间 [配置使用Yii2 autoloader]

    Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同.这篇文章讨论一下如何利用Yii 2.0的自动加载机制,向系统中引入自定义类和命名空间.本文旨在抛砖引玉,如 ...

  3. c# Json 自定义类作为字典键时,序列化和反序列化的处理方法

    一般情况下,Newtonsoft.Json.dll 对 Dictionary<int,object>.Dictionary<string,object>等序列化与反序列化都是成 ...

  4. JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  5. Extjs 学习总结-Ext.define自定义类

    本教程整理了extjs的一些基本概念及其使用,包括自定义类(Ext.define).数据模型.代理等.本节介绍使用Ext.define自定义类 使用Ext.define自定义类 1. 首先看看js中自 ...

  6. ExtJS 中自定义类

    首先我们来看一看在Javascript中,是怎样自定义类的: var Person = function (name, age) { this.Name = ""; this.Ag ...

  7. 【C#】第3章学习要点(二)自定义类和结构

    分类:C#.VS2015 创建日期:2016-06-19 使用教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.要点概述 别人提供的类都是为了简化你的工作量用的,可是 ...

  8. Python中使用自定义类class作为海量数据结构时内存占用巨大的问题

    最近碰到处理一个二十多兆的文件时内存蹭蹭的吃掉四百多兆,吓死宝宝了. 无奈毕竟接触python时间有限,还没有完整的看过python的一些基础知识,我想一个合格的pythoner应该不会碰到这个问题. ...

  9. cocos2dx-3.x 导出自定义类到 lua 过程详解

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.简介 最近正在学习cocos2d中的lua游戏开发,因为lua开发的热更新特性,大家开发游戏好像都会优先选择lua作为 ...

随机推荐

  1. php socket函数详解

    最近在用socket实现服务端向客户端主动推送消息 函数名 描述 socket_accept() 接受一个Socket连接 socket_bind() 把socket绑定在一个IP地址和端口上 soc ...

  2. Oracle存储过程单步调试方法

    oracle存储过程单步调试的方法 1.在要调试的过程上单击test,如下图所示: 2.出现如下界面时单击最左上方的按钮:,如下图所示: 3.单击后呈现如下画面: 其中: 表示要停止test; 表示要 ...

  3. snackbar初体验

    底部弹出的部分就是snackbar的,右侧可添加一个action响应点击事件,遗憾的时貌似只能添加一个 这是华为mate8上面运行出来的效果,颜色之类的都是默认.不同的android版本样式稍有差异 ...

  4. hbase 停止regionserver

    每个regionserver节点可以自由启动或停止,可以不随hbase整体一起. 停止后regionserver上的数据会被移到其他regionserver上,不影响hbase的使用.   停止reg ...

  5. maven 手动安装jar到仓库的命令

    mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.6.0 -Dpackaging=jar -Dfile ...

  6. Area(Pick定理POJ1256)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5429   Accepted: 2436 Description ...

  7. js获取url值

    // 获取地址栏的参数数组 function getUrlParams() { var search = window.location.search; // 写入数据字典 var tmparray ...

  8. JAVA基础知识之多线程——线程池

    线程池概念 操作系统或者JVM创建一个线程以及销毁一个线程都需要消耗CPU资源,如果创建或者销毁线程的消耗源远远小于执行一个线程的消耗,则可以忽略不计,但是基本相等或者大于执行线程的消耗,而且需要创建 ...

  9. Mac环境下用Java(Sikuli+Robot)实现页游自动化

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ Sikulix(以前叫Sikuli)在Mac电脑的环境配置步骤如下: 1.从官网上下载Sikuli ...

  10. 在repeater增加自增长的序号

    <td><%#Container.ItemIndex+ %></td> 完!!!