上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象。那么现在就让我们使用AspectJ注解来定义切面吧。

1.定义切面

下面我们就来定义一场舞台剧中观众的切面类Audience:

package com.spring.aop.service.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; /**
* <dl>
* <dd>Description:观看演出的切面</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月3日 下午9:58:09</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@Aspect
public class Audience { /**
* 目标方法执行之前调用
*/
@Before("execution(** com.spring.aop.service.perform(..))")
public void silenceCellPhone() {
System.out.println("Silencing cell phones");
} /**
* 目标方法执行之前调用
*/
@Before("execution(** com.spring.aop.service.perform(..))")
public void takeSeats() {
System.out.println("Taking seats");
} /**
* 目标方法执行完后调用
*/
@AfterReturning("execution(** com.spring.aop.service.perform(..))")
public void applause() {
System.out.println("CLAP CLAP CLAP");
} /**
* 目标方法发生异常时调用
*/
@AfterThrowing("execution(** com.spring.aop.service.perform(..))")
public void demandRefund() {
System.out.println("Demanding a refund");
} }

我们可以看到使用了几种注解,其实AspectJ提供了五中注解来定义通知:

注解 通知
@After 通知方法会在目标方法返回或抛出异常后调用
@AfterRetruening 通常方法会在目标方法返回后调用
@AfterThrowing 通知方法会在目标方法抛出异常后调用
@Around 通知方法将目标方法封装起来
@Before 通知方法会在目标方法执行之前执行

  聪明的你可能已经看到,同样的切点我们写了四遍,这是不科学的,强大的Spring怎么会没有处理的方法呢。其实我们可以使用@Pointcut注解声明一个通用的切点,在后面可以随意使用:

package com.spring.aop.service.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; /**
* <dl>
* <dd>Description:观看演出的切面</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月3日 下午9:58:09</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@Aspect
public class Audience { /**
* 定义一个公共的切点
*/
@Pointcut("execution(** com.spring.aop.service.Perfomance.perform(..))")
public void performance() {
} /**
* 目标方法执行之前调用
*/
@Before("performance()")
public void silenceCellPhone() {
System.out.println("Silencing cell phones");
} /**
* 目标方法执行之前调用
*/
@Before("performance()")
public void takeSeats() {
System.out.println("Taking seats");
} /**
* 目标方法执行完后调用
*/
@AfterReturning("performance()")
public void applause() {
System.out.println("CLAP CLAP CLAP");
} /**
* 目标方法发生异常时调用
*/
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Demanding a refund");
} }

  这样定义一个切点后,后面我们的方法想使用这个切点直接调用切点所在的方法就行了。实际上切面也是一个Java类,我们可以将它装配到Spring中的bean中:

/**
* 声明Audience bean
* @return
*/
@Bean
public Audience audience(){
return new Audience();
}

  但是现在Spring还不会将Audience视为一个切面,即便使用了@AspectJ注解,但它并不会被视为一个切面们这些注解不会被解析,也不会创建将其转化为切面的代理。但我们可以使用JavaConfig,然后在JavaConfig类上使用注解@EnableAspectJAutoProxy注解启动自动代理功能:

package com.spring.aop.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.spring.aop.service.aop.Audience; /**
* <dl>
* <dd>Description:配置类</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月3日 下午10:20:11</dd>
* <dd>@author:Kong</dd>
* </dl>
*/ @Configuration
//启动AspectJ自动代理
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig { /**
* 声明Audience bean
* @return
*/
@Bean
public Audience audience(){
return new Audience();
} }

如果你想使用XML配置也是可以的,我们要使用Spring aop命名空间中的<aop:aspectj-autoproxy>元素:

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <context:component-scan base-package="com.spring.aop" />\ <!-- 启动AspectJ自动代理 -->
<aop:aspectj-autoproxy/> <bean class="com.spring.aop.Audience" />
</beans>

  其实不管使用JAvaConfig还是Xml,AspectJ都会为使用@ApsectJ注解的Bean创建一个代理,这个代理会环绕着所有该切面所匹配的bean。

2.创建环绕通知

  环绕通知是这几种通知中相对复杂的一种,它可以在一个同时中同时编写前置和后置通知:

/**
* 环绕通知
* @param jp 通过它调用目标方法
*/
@Around("perforance()")
public void watchPerformance(ProceedingJoinPoint jp) { try {
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP!!!");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}

3.处理通知中的参数

  其实我们可以使用指示器args为切面中的方法传递参数,比如我们想统计唱片中每首歌曲的播放次数,这时我们想使用切面来完成这个工作,我们就需要向切面传递一个歌曲的编号,这时我们可以通过args向切面中的方法传递参数:

package com.spring.aop.service.aop;

import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; /**
* <dl>
* <dd>Description:统计每首歌曲播放次数的AOP</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月4日 上午8:19:24</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@Aspect
public class TrackCounter { private Map<Integer,Integer> trackCounts = new HashMap<Integer,Integer>(); @Pointcut("execution(* com.spring.aop.service.CompactDisc.playTrack(int)) && args(trackNumber)")
public void trackPlayed(int trackNumber){} @Before("trackPlayed(trackNumber)")
public void countTrack(int trackNumber){
int currentCount = getPlayCount(trackNumber);
trackCounts.put(trackNumber, currentCount+1);
} public int getPlayCount(int trackNumber) {
return trackCounts.containsKey(trackNumber)?trackCounts.get(trackNumber):0; } }

然后我们将这个切面配置到Spring中:

package com.spring.aop.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.google.common.collect.Lists;
import com.spring.aop.service.CompactDisc;
import com.spring.aop.service.aop.TrackCounter;
import com.spring.aop.service.impl.BlankDisc; /**
* <dl>
* <dd>Description:显示配置播放歌曲的bean和记录播放次数的AOP</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月4日 上午8:52:05</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@Configuration
@EnableAspectJAutoProxy
public class TrackCounterConfig { @Bean
public CompactDisc sgtPeppers(){
BlankDisc cd = new BlankDisc();
cd.setTitle("Sgt. Pepper's Lonely Hearts Club Band");
cd.setArtist("The Beatles"); List<String> tracks = Lists.newArrayList();
tracks.add("Sgn. Pepper's Lonelu Hears Club Band");
tracks.add("Wiith a Litter Help from My Friends");
tracks.add("Lucy in the Sky with Diamonds");
tracks.add("Getting Better");
tracks.add("Fixing a Hole"); //...other tracks omitted for brevity ...
cd.setTracks(tracks);
return cd;
} @Bean
public TrackCounter trackCOunter(){
return new TrackCounter();
}
}

为了证明他能够正常运行我们编写一个测试类进行测试:

package service;

import static org.junit.Assert.assertEquals;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.spring.aop.config.TrackCounterConfig;
import com.spring.aop.service.CompactDisc;
import com.spring.aop.service.aop.TrackCounter; /**
* <dl>
* <dd>Description: 测试统计歌曲播放的AOP</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月4日 上午9:08:09</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCounterConfig.class)
public class TrackCounterTest { @SuppressWarnings("deprecation")
@Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired
private CompactDisc cd; @Autowired
private TrackCounter counter; @Test
public void testTrackCounter(){
cd.playTrack(1);
cd.playTrack(2);
cd.playTrack(3);
cd.playTrack(4);
cd.playTrack(2);
cd.playTrack(4);
cd.playTrack(2); assertEquals(1,counter.getPlayCount(1));
assertEquals(3,counter.getPlayCount(2));
assertEquals(1,counter.getPlayCount(3));
assertEquals(2,counter.getPlayCount(4));
assertEquals(0,counter.getPlayCount(5));
assertEquals(0,counter.getPlayCount(0));
}
}

运行后确定我们的代码是正确的!

4.通过注解引入新功能

  像Java这种静态语言,一般一般类定义完成后,类中的方法属性就已经确定了,如果我们想要为这些类添加新方法、功能,就可以使用Spring Aop。现在我们想为所有的Performance实现引入下面的Encoreable接口:

package com.spring.aop.service;
/**
* <dl>
* <dd>Description:添加新功能</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:2016年9月4日 上午9:48:36</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
public interface Encoreable { void performEncore();
}

要实现该功能我们要创建一个新的切面:

package com.spring.aop.service.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents; import com.spring.aop.service.Encoreable;
import com.spring.aop.service.impl.DefaultEncoreable; /**
* <dl>
* <dd>Description:为目标bean添加新功能的AOP bean</dd>
* <dd>Company: 黑科技</dd>
* <dd>@date:201
6年9月4日 上午9:45:34</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@Aspect
public class EncoreableIntroducer { @DeclareParents(value="com.spring.aop.service.Perforance+",defaultImpl=DefaultEncoreable.class)
public static Encoreable encoreable; }

我们可以看到在切面中它并没有使用前置、后置等通知的注解,而是使用了@DeclareParents注解,将Encoreable接口引入到Performance bean中。
@DeclareParents注解由三部分组成:

  • value属性指定了哪种类型的bean要引入该接口(加号表示是Performance的所有子类)。
  • defaultImpl属性指定了为引入功能提供实现类
  • @DeclareParents注解所标注的静态属性指明了要引入了接口,在这里,我们所引入的是Encoreable接口。

和其他切面一样,我们需要在Spring应用中将EncoreableIntroducer声明为一个bean:
<bean class="com.spring.aop.service.aop.EncoreableIntroducer" />
至此在Java类中配置切面的内容已经全部介绍完了

Spring AOP之使用注解创建切面的更多相关文章

  1. SpringInAction--面向切片的Spring以及如何使用注解创建切面

    什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...

  2. Spring AOP事务管理(使用切面把事务管理起来)

    在<Spring Transaction 分析事务属性(事务的基本概念.配置)>基础上 http://blog.csdn.net/partner4java/article/details/ ...

  3. Spring Boot 2.X(八):Spring AOP 实现简单的日志切面

    AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...

  4. 运用Spring Aop,一个注解实现日志记录

    运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...

  5. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

  6. 利用Spring AOP和自定义注解实现日志功能

    Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...

  7. 5.2 spring5源码--spring AOP源码分析二--切面的配置方式

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  8. Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志

    一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...

  9. Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式

    背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前 ...

随机推荐

  1. MySQL--教程

    登入登出 首先启动服务,然后 mysql -u root -p 命令输入密码登入. mysql退出三种方法:mysql > exit;mysql > quit;mysql > \q;

  2. C#调用VP 包含素材

    VS2012 +VP9.0 ***************** 自己运行的时只要修改VP里面素材的路径即可 链接: https://pan.baidu.com/s/1J6Bc5FcBYLZLgqe30 ...

  3. selenium webdriver显示等待时间

    当页面加载很慢时,推荐使用显示等待:等到需要操作的那个元素加载成功之后就直接操作这个元素,不需要等待其他元素的加载 WebDriverWait wait = new WebDriverWait(dri ...

  4. Liferay中request

    在liferay中的请求分为renderRequest和actionRequest这两种请求的方式,portletRequest的子类有三个1renderRequest,2EventRequest3C ...

  5. js中数组的字符串表示

    <html> <head> <title>数组的字符串表示</title> <script type="text/javascript& ...

  6. 不用中间变量交换a 和b的值

    // 不用中间变量的写法 ,假如 a=13, b=8; a=a+b =21; //此时 a=21; b=8; b=a-b=13; //此时a=21; b=13; a=a-b=8; //相当于 a=21 ...

  7. 55. Jump Game(贪心)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 无法在web服务器下启动调试

    VS2013 编译时异常 无法在web服务器下启动调试.该Web服务器未及时响应   VS2013 编译时错误 无法在web服务器上启动调试.该Web服务器未及时响应.无法在web服务器上启动调试.该 ...

  9. python 的math模块

    数学模块用法:import math# 或 from math import * 变量 描述 math.e 自然对数的底e math.pi 圆周率pi 函数名 描述 math.ceil(x) 对x向上 ...

  10. mysql的数据类型和字符集

    MySQL的数据类型 MySQL数据库支持的数据类型主要有以下几种: 整型 浮点型 字符 BLOB型 枚举和集合类型 JSON类型(MySQL5.7新增加的支持) 整型 整数类型是数据库中最基本的数据 ...