在了解spring aop中的关键字(如:连接点(JoinPoint)、切入点(PointCut)、切面(Aspact)、织入(Weaving)、通知(Advice)、目标(Target)等)后进行了简单使用测试。

1.在spring的xml配置文件中添加代码,开启aop注解自动代理

<!-- 启动aspectJ自动代理 -->
<aop:aspectj-autoproxy />

2.创建一个方法作为连接点:

public class TestService {

    public void method1(){
System.out.println("执行method1");
}
}

3.创建切面类(注解方式配置):

@Aspect// 注解该类为切面类
@Component
public class LogIntercepter { // 若要以字符串形式表示执行目标条件必须使用final修饰
private final String EXEC = "execution(* com.lwl.service..*.*(..))"; // 执行顺序:环绕通知(前),前置通知,环绕通知(后),后置通知,返回后通知
@Pointcut("execution(* com.lwl.service..*.*(..))")// 切入点
public void pointcutMethod(){}; @Before("pointcutMethod()")// 方法执行前执行
public void before(){
System.out.println("method before");
} @After("pointcutMethod()")// 方法执行后执行
public void after(){
System.out.println("method after");
} @AfterReturning("pointcutMethod()")// 方法返回后执行
public void afterReturning(){
System.out.println("method afterReturning");
} @AfterThrowing("pointcutMethod()")// 方法抛异常后执行
public void afterThrowing(){
System.out.println("method afterThrowing");
} @Around("pointcutMethod()")// 环绕通知
public void around(ProceedingJoinPoint pr){
System.out.println("method around bg");
try {
pr.proceed();// 执行目标方法
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("method around ed");
}
}

调用method1()后通知的打印先后顺序结果为:

method around bg
method before
执行method1
method around ed
method after
method afterReturning

Spring AOP xml方式配置,在Spring的xml配置文件中加入(备忘):


<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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 1.注册实体类(目标方法所在类) -->
<bean id="userDao" class="com.huitong.Dao3.UserDao"></bean>
<!-- 2.注册AOP切面实体类(通知方法所在类) -->
<bean id="aop" class="com.huitong.Dao3.Aop"></bean> <aop:config>
<!-- 3.切入点配置 -->
<aop:pointcut expression="execution(* com.huitong.Dao3.UserDao.*(..))" id="pt"/>
<!-- 4.切面配置 -->
<aop:aspect id="asp" ref="aop">
<!-- 5.通知配置 -->
<aop:around method="around" pointcut-ref="pt"/>
<aop:before method="before" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>

AOP相关的术语:

    • 通知: 通知定义了切面是什么以及何时使用的概念。Spring 切面可以应用5种类型的通知:

      • 前置通知(Before):在目标方法被调用之前调用通知功能。
      • 后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么。
      • 返回通知(After-returning):在目标方法成功执行之后调用通知。
      • 异常通知(After-throwing):在目标方法抛出异常后调用通知。
      • 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。
    • 连接点:是在应用执行过程中能够插入切面的一个点。

    • 切点: 切点定义了切面在何处要织入的一个或者多个连接点。
    • 切面:是通知和切点的结合。通知和切点共同定义了切面的全部内容。
    • 引入:引入允许我们向现有类添加新方法或属性。
    • 织入:是把切面应用到目标对象,并创建新的代理对象的过程。切面在指定的连接点被织入到目标对象中。在目标对象的生命周期中有多个点可以进行织入: 
      • 编译期: 在目标类编译时,切面被织入。这种方式需要特殊的编译器。AspectJ的织入编译器就是以这种方式织入切面的。
      • 类加载期:切面在目标加载到JVM时被织入。这种方式需要特殊的类加载器(class loader)它可以在目标类被引入应用之前增强该目标类的字节码。
      • 运行期: 切面在应用运行到某个时刻时被织入。一般情况下,在织入切面时,AOP容器会为目标对象动态地创建一个代理对象。SpringAOP就是以这种方式织入切面的。

最新文章

  1. 来玩Play框架01 简介
  2. tomcat开机启动
  3. 执行SSIS Package的三种方式
  4. c#链接数据库
  5. node.js事件触发
  6. Unity场景道具模型拓展自定义编辑器
  7. JavaScript判断IE各版本最完美解决方案
  8. css 串联选择器和后代选择器
  9. 他的第一个NDK的Demo
  10. java 随机流
  11. 简单模拟一下ab压力测试
  12. JS window对象的top、parent、opener含义介绍(转载)
  13. 算法——八皇后问题(eight queen puzzle)之回溯法求解
  14. asp.net core 系列 20 EF基于数据模型创建数据库
  15. SSL双向认证和SSL单向认证的流程和区别
  16. css -html-文档流
  17. python学习笔记6--mockserver
  18. 关于开发板用tftp下载失败分析
  19. COGS 513 八
  20. go 算法 查询字符在字符串中的位置

热门文章

  1. web项目tomcat启动url自定义(去掉项目名)
  2. 【杂谈】RN的一点回顾与未来的展望
  3. 使用方向变换(directional transform)图像分块压缩感知
  4. 16-2 基于localStorage或sessionStorage的计数器
  5. [转]F# Samples 101 - Visual Studio 2010
  6. Java-超市购物小票案例-详细介绍
  7. Java 208道面试题及部分答案
  8. vultr系统重建
  9. webstorm 设置ES6语法支持以及添加vuejs开发配置
  10. B5. Concurrent JVM 锁优化