二十9天 月出冲击黑鸟 —Spring的AOP_AspectJ @annotation
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); @Test
public 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。
文件夹结构例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvendzendz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
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的更多相关文章
- Spring Boot入门系列(二十六)超级简单!Spring Data JPA 的使用!
之前介绍了Mybatis数据库ORM框架,也介绍了使用Spring Boot 的jdbcTemplate 操作数据库.其实Spring Boot 还有一个非常实用的数据操作框架:Spring Data ...
- 二十六:Struts2 和 spring整合
二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...
- spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求 有半年多没有更新了,按照常规剧本,应该会说项目很忙,工作很忙,没空更新,吧啦吧啦,相关的话吧, 但是细想想 ...
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...
- SpringBoot开发二十-Redis入门以及Spring整合Redis
安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...
- 设计模式学习(二十四):Spring 中使用到的设计模式
设计模式学习(二十四):Spring 中使用到的设计模式 作者:Grey 原文地址: 博客园:设计模式学习(二十四):Spring 中使用到的设计模式 CSDN:设计模式学习(二十四):Spring ...
- Spring源码分析(二十二)功能扩展
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.增加SPEL语言的支持 二.增加属性注册编辑器 1. 使用自 ...
- SpringBoot进阶教程(二十六)整合Redis之共享Session
集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...
- SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用
在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...
随机推荐
- 内网port映射具体解释(花生壳)
关于怎样建立服务器的解答. 一.花生壳的作用 首先,我们先来了解一下花生壳的究竟有什么作用.由于ADSL每次拨号上网所获得的IP地址每次都是不同的,花生壳起到的作用就是方便用户訪问我们的server( ...
- 宏碁宣布Liquid Jade智能机和Leap袖口
据科技网站Android Community 4月29日覆盖,宏碁29公布的新智能机Liquid Jade而随着智能手镯部署Liquid Leap.尽管宏碁已经宣布了一项新的外部基本信息.但价格格和商 ...
- poj 3399 Product(数学)
主题链接:http://poj.org/problem?id=3399 Product Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- uva10791 uva10780(分解质因数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- (hdu 7.1.8)Quoit Design(最低点——在n一个点,发现两点之间的最小距离)
主题: Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- atitit.报告最佳实践oae 和报告引擎的选择
atitit.报告最佳实践oae 与报表引擎选型 1. 报表的基本的功能and结构 2 1.1. 查询设计器(配置化,metadata in html) ,anno 2 1.2. 查询引擎 2 1.3 ...
- Android4.0设置接口变更摘要(四)
为了与你之前,你已经设置了共享Tab风格和Item实现圆角.希望能给有须要的朋友一点点帮助,今天再和大家分享一下用ViewPager实现设置分页,小米和OPPO就是这种设置,先来看看效果图: wate ...
- C++写一个简单的解析器(分析C语言)
该方案实现了一个分析C语言的词法分析+解析. 注意: 1.简单语法,部分秕.它可以在本文法的基础上进行扩展,此过程使用自上而下LL(1)语法. 2.自己主动能达到求First 集和 Follow 集. ...
- ffmpeg架构和解码流程分析
转 一,ffmpeg架构 1. 简介 FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的 开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用 ...
- Android游戏源代码合集(主要是AndEngine和Libgdx的)
近期在网络上看到有网友抱怨Android游戏源代码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源代码,以Eclipseproject的形式配置好环境,再陆续发出(某引擎避嫌,不在此 ...