场景描述:

  核心业务:举行一场古典音乐会。

  周边功能:观众入场,关闭手机、落座,觉得音乐好听时鼓掌,觉都不好听则退票。(切面)

1.编写切点(切点用于准确定位应该在什么地方应用切面的通 知)————即核心业务

首先定义一个Performance接口:

 package concert;

 public interface Performance {
public void perform();
}

2.定义切面,即编写Audience.java。

Audience类使用@Aspect注解进行了标注。该注解表明Audience不仅仅是一个POJO,还是一个切面。

@Pointcut注解能够在一 个切面内定义可重用的切点。

也可以在通知注解中直接使用切点表达式。

 package concert;

 import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class Audience {
@Pointcut("execution(** concert.Performance.perform(..))")
public void perform() { } // @Before("execution(** concert.Performance.perform(..))")
@Before("perform()")
public void silenceCellPhones() {
System.out.println("Silencing cell phone");
} // @Before("execution(** concert.Performance.perform(..))")
@Before("perform()")
public void takeSeats() {
System.out.println("Taking seats");
} // @AfterReturning("execution(** concert.Performance.perform(..))")
@AfterReturning("perform()")
public void applause() {
System.out.println("CLAP CLAP CLAP");
} // @AfterThrowing("execution(** concert.Performance.perform(..))")
@AfterThrowing("perform()")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}

Audience有四个方法,定义了一个观众在观看演出时可能会做的事 情。在演出之前,观众要就坐(takeSeats())并将手机调至静音 状态(silenceCellPhones())。如果演出很精彩的话,观众应 该会鼓掌喝彩(applause())。不过,如果演出没有达到观众预期 的话,观众会要求退款(demandRefund())。 这些方法都使用了通知注解来表明它们应该在什么时候调用。

在这里也可以使用环绕通知,环绕通知是最为强大的通知类型。它能够让你所编写的逻辑将被通知 的目标方法完全包装起来。实际上就像在一个通知方法中同时编写前 置通知和后置通知,代码如下:

      @Around("performs()") // 环绕通知方法
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phone");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}

在这里,@Around注解表明watchPerformance()方法会作 为performance()切点的环绕通知。ProceedingJoinPoint作为参数。这个对象是必须要有的,因为要在通知中通过它来调用被通知的方法。通知方法中可以做任何的 事情,当要将控制权交给被通知的方法时,它需要调 用ProceedingJoinPoint的proceed()方法。

<可以不调用proceed()方法,从而阻塞对被通知方 法的访问,与之类似,也可以在通知中对它进行多次调用。要这样 做的一个场景就是实现重试逻辑,也就是在被通知方法失败后,进行 重复尝试。>

3.定义Java配置文件ConcertConfig.java,使用Spring自动装配。在Java配置文件中启用AspectJ注解的自动代理。

AspectJ自动代理都会为使 用@Aspect注解的bean创建一个代理,这个代理会围绕着所有该切面 的切点所匹配的bean。

 package concert;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@EnableAspectJAutoProxy // 启用AspectJ自动代理
@ComponentScan
public class ConcertConfig { }

也可以定义XML配置文件

在Spring中要使用XML来装配bean的话,那么需要使用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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="concert"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--启用自动代理-->
<!-- <bean class="concert.Audience"></bean>
<bean class="concert.Classcial"></bean> -->
</beans>

注:在使用XML进行装配的时候,如果在XML声明了bean后,一定要去掉两个bean原来的@Component注解,且不用使用自动代理;如果不在XML文件中声明bean,在Audience和Classcial中添加@Component注解,则可以启用自动代理。

4.编写测试文件ConcertTest.java

package concert;

import org.junit.Test;
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; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = concert.ConcertConfig.class)
public class ConcertTest {
@Autowired
private Performance perform; @Test
public void test() {
perform.perform();
}
}

疑问:在进行测试时,只能定义接口的对象来进行测试,定义接口的实现类对象时就会报错。

5.结果

两种配置方式结果一样

最新文章

  1. 【新手学Python】一、基础篇
  2. javascript选择排序
  3. HDU 5807 Keep In Touch DP
  4. [Node.js] Node + Redis 实现分布式Session方案
  5. STL 自学
  6. POJ C++程序设计 编程题#4:魔兽世界之一:备战
  7. WCF之事务
  8. /mnt /media /dev 目录区别
  9. js获取location.href的参数实例代码
  10. ZipArchive 的使用
  11. 【转】win7 旗舰版激活密钥
  12. maven生成war包的两种方式
  13. makefile 学习一
  14. 前端回答从输入URL到页面展示都经历了些什么
  15. 快速删除C#代码中的空白行
  16. oracle连接数据库和连接表的操作
  17. alpha冲刺(6/10)
  18. asp.net core2.0中网站发布的时候,怎么样才配置才可以使视图文件不被打包进去?
  19. Android事件总线(三)otto用法全解析
  20. JS 显示隐藏DIV

热门文章

  1. RESTful三问
  2. React 深入系列1:React 中的元素、组件、实例和节点
  3. 帧动画的创建方式 - 纯Java代码方式
  4. 回收 PV - 每天5分钟玩转 Docker 容器技术(152)
  5. Angular 学习笔记 ( CDK - Observers )
  6. angular2 学习笔记 ( 第3方插件 jQuery and ckeditor )
  7. redis入门(05)redis的key命令
  8. hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(3)http://www.cnblogs.com/liugh/p/6624491.html
  9. jQuery serialize()方法获取不到数据,alert结果为空
  10. golang-在gin中cookie跨域设置(配合ajax)