6月14日,阴转雨。

“四面垂杨十里荷,向云何处最花多, 画楼南畔夕阳和。天气乍凉人寂寞, 光阴须得酒消磨,且来花里听笙歌。”

面向切面的框架AspectJ邂逅Spring,不仅造就一种简洁,更带来很多其它的选择空间。

上篇的切面与代理配置方式。麻烦且繁琐。好在Spring 与 AspectJ 进行了集成,从接口和配置的泥潭爬出,善莫大焉。

以下把上一篇Spring的AOP 的样例改写一下,达到相同的效果。

1-3步骤不变。
       4、定义一个 Aspect 切面类BallHandler.java

package edu.eurasia.aop;import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.AfterReturning;  @Aspect
public class BallHandler {static Logger logger = Logger.getLogger(TestBall.class);@Pointcut("execution(* edu.eurasia.aop.WatchBallImpl.watchfoot*(..))")  public void watchfootball(){};@Pointcut("execution(* edu.eurasia.aop.WatchBallImpl.watchbasket*(..))")  public void watchbasketball(){};@Before(value="watchfootball()&& args(city,..)")  public void watchBefore(String city){  logger.debug(" 看球前,先去  " + city + "  咖啡馆喝杯巴西咖啡 ...");}  @AfterReturning("watchbasketball() && args(city,..)")  public void watchAfter(String city){  logger.debug("看完球去  " + city + "  麦当劳吃汉堡包!

"); } }<span style="background-color: rgb(255, 255, 255);"> </span>

类上面标注的 @Aspect 注解。这表明该类是一个 Aspect(事实上就是 Advisor)-切面类。

该类无需实现不论什么的接口,仅仅需定义一个方法(方法叫什么名字都无所谓)。

@Pointcut使用这种方法能够将edu.eurasia.aop.WatchBallImpl.watchfoot*(..)方法声明为poincut即切入点。作用,在watchfoot*(..)方法被调用的时候运行watchfootball()方法。当中execution是匹配方法运行的切入点。也就是spring最经常使用的切入点定义方式。

@Before(value="watchfootball()&& args(city,..)):@Before声明为在切入点方法运行之前运行,而后面没有直接声明切入点,而是value="watchfootball()",是由于假设@afterReturning等都有所修改的时候都必须所有修改。所以统一用Pointcut的watchfootball取代,这样子有修改的时候仅需修改一个地方。其他@AfterReturning类同。&& args(city,..)能够获取切入点方法watchfoot*(..)的參数。

以下分析一下这个切点表达式:

execution(* edu.eurasia.aop.WatchBallImpl.watchfoot*(..))

  • execution():表示拦截方法,括号里可定义须要匹配的规则。

  • 第一个“*”:表示方法的返回值是随意的。

  • 第二个“*”:表示匹配该类中全部的方法。

  • (..):表示方法的參数是随意的。

5、编写spring的配置文件applicationContext.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-2.0.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
><!-- 定义Aspect -->   <bean id="ballHandler" class="edu.eurasia.aop.BallHandler" /><!-- 定义Common -->   <bean id="watchBall" class="edu.eurasia.aop.WatchBallImpl" />   <!-- 启动AspectJ支持 -->   <aop:aspectj-autoproxy />
</beans>

6、编写測试类TestBall.java

package edu.eurasia.aop;import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBall {static Logger logger = Logger.getLogger(TestBall.class);@Testpublic void testball() {logger.debug("test begin ");ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");WatchBall watchball = (WatchBall) ctx.getBean("watchBall");watchball.watchfootball("里约热内卢");logger.debug("----------------------------");watchball.watchbasketball("德克萨斯州");}
}

7、执行结果和环境配置

执行结果例如以下:

DEBUG [main] (BallHandler.java:21) -  看球前,先去  里约热内卢  咖啡馆喝杯巴西咖啡 ...
DEBUG [main] (WatchBallImpl.java:11) - 去 里约热内卢 看2014巴西世界杯
DEBUG [main] (TestBall.java:25) - ----------------------------
DEBUG [main] (WatchBallImpl.java:16) -  去德克萨斯州 看2014NBA总决赛

DEBUG [main] (BallHandler.java:26) - 看完球去  德克萨斯州  麦当劳吃汉堡包。

本例使用spring 2.5.6,除了找出spring.jar。commons-logging-1.1.1.jar两个jar包,外加一个log4j.jar。还要下载aspectj-1.7.0.jar。解压后有四个JAR:aspectjrt.jar,aspectjtools.jar,aspectjweaver.jar和org.aspectj.matcher.jar。还须要下载一个aopalliance-1.0.jar。

文件夹结构例如以下:

8、基于配置的AspectJ

除了使用 @Aspect 注解来定义切面类以外,Spring AOP 也提供了基于配置的方式来定义切面类。

(1) 配置文件applicationContext.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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!-- 定义Aspect --> <bean id="ballHandler" class="edu.eurasia.aop.BallHandler" /> <!-- 定义Common --> <bean id="watchBall" class="edu.eurasia.aop.WatchBallImpl" /> <aop:config> <aop:aspect ref="ballHandler"> <aop:before method="watchBefore" arg-names="city" pointcut="execution(* edu.eurasia.aop.WatchBallImpl.watchfoot*(String,..)) and args(city,..)" /> <aop:after method="watchAfter" arg-names="city" pointcut="execution(* edu.eurasia.aop.WatchBallImpl.watchbasket*(String,..)) and args(city,..)" /> </aop:aspect> </aop:config> </beans>

(String,..)声明切入点至少含有一个String类型的參数。显然能够匹配WatchBallImpl中的watchfoot*(String city,..);args(n,..)声明给watchfoot*(String city,..)的第一个參数起了个别名“n”传递给Advice,假设<aop:before...>中arg-names不是“n”,将抛出异常。

(2) BallHandler.java 代码变化不大:

package edu.eurasia.aop;import org.apache.log4j.Logger;  public class BallHandler {static Logger logger = Logger.getLogger(TestBall.class);public void watchBefore(String city){  logger.debug(" 看球前。先去  " + city + "  咖啡馆喝杯巴西咖啡 ...");}  public void watchAfter(String city){  logger.debug("看完球去  " + city + "  麦当劳吃汉堡包!");}
}

(3)其余代码和配置不变,执行结果同样。

版权声明:本文博主原创文章,博客,未经同意不得转载。

二十9天 月出冲击黑鸟 —Spring的AOP_AspectJ @annotation相关推荐

  1. 二十六:Struts2 和 spring整合

    二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...

  2. Spring Cloud 2.2.2 源码之二十九nacos客户端获取配置原理四

    Spring Cloud 2.2.2 源码之二十九nacos客户端获取配置原理四 MetricsHttpAgent的httpGet ServerHttpAgent的httpGet HttpSimple ...

  3. Spring Batch之读数据—FlatFileItemReader(二十五)

    FlatFileItemReader实现ItemReader接口,核心作用将Flat文件中的记录转换为Java对象. 一.FlatFileItemReader中关键属性 属性 类型 说明 buffer ...

  4. Spring Batch之读数据—读JSON文件(二十八)

    一.JSON文件 Spring Batch之读数据-Flat格式文件(二十四)_人--杰的博客-CSDN博客_flat文件 二.项目实例 1.项目框架 2.代码实现 (1)BatchMain.java ...

  5. Spring Batch之读数据—读分隔符文件(二十六)

    一.分隔符文件 Spring Batch之读数据-Flat格式文件(二十四)_人--杰的博客-CSDN博客_flat文件 二.项目实例 1.项目框架 2.代码实现 (1)启动类BatchMain.ja ...

  6. Spring Boot教程(二十):Spring Boot使用String Task定时任务

    一.JAVA常见的几种定时任务比较 Timer:jdk自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让程序按照某一个频度执行, ...

  7. Spring Boot + vue-element 开发个人博客项目实战教程(二十五、项目完善及扩展(前端部分))

    ⭐ 作者简介:码上言 ⭐ 代表教程:Spring Boot + vue-element 开发个人博客项目实战教程 ⭐专栏内容:零基础学Java.个人博客系统 ⭐我的文档网站:http://xyhwh- ...

  8. Java笔记二十四——Spring开发

    Spring是一个支持快速开发Java EE应用程序的框架.它提供了一系列底层容器和基础设施,并可以和大量常用的开源框架无缝集成,可以说是开发Java EE应用程序的必备. 在Spring Frame ...

  9. TIOBE 8 月榜单:Groovy 和 Objective-C 重返前二十

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | https://www.oschina.net ...

最新文章

  1. Html.BeginForm 与Section、Partial View 和 Child Action
  2. Java并发编程之线程同步
  3. windows mysql.tar安装_MySQL的安装
  4. mybatis学习(54):鉴定器
  5. 【NLP_Stanford课堂】语言模型1
  6. 远程控制篇:抓取远程屏幕图像
  7. vue element序号翻页连续排序
  8. Griddle, griddle-react 一个REACT 表格组件
  9. 数据清洗+特征构造:bureau.csv
  10. 学生寝室管理系统-C语言版
  11. html吃豆豆游戏代码,HTML5 Canvas 来回简单版吃豆豆
  12. jbutton如何实现点击_Java Swing JButton按钮的实现示例
  13. KVM地址翻译流程及EPT页表的建立过程
  14. VIRTIO-BLK设备SERIAL ID
  15. ToC产品和ToB产品的区别
  16. 数据库设计之备用字段
  17. 20170322埃森哲电话面试
  18. 想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
  19. 消防报警系统服务器,消防报警系统
  20. 4种常见的osgi框架比较

热门文章

  1. 十步杀一人,千里不留行
  2. 88NV1120主控,镁光颗粒无法格式化、掉盘、开卡失败的偶然解决(修复)方法
  3. 7.完善BootLoader功能
  4. 星辰小队针对于软件“星遇”的第二次10天冲刺——第2天
  5. 王东岳:中国文化绝不产生科学
  6. 5G在中国一步步满血,高通实现毫米波独立组网:7.1Gbps网速、3.6毫秒延迟
  7. 安卓15作业 - 制作个人相册
  8. 服务器任务管理器详细信息,服务器怎么打开任务管理器
  9. 次时代游戏建模人物制作经验分享
  10. idea 配置profiles