AspectJ支持5种类型的通知注解:

  • @Before:前置通知,在方法执行之前执行;
  • @After:后置通知,在方法执行之后执行;
  • @AfterRunning:返回通知,在方法返回结果之后执行(因此该通知方法在方法抛出异常时,不能执行);
  • @AfterThrowing:异常通知,在方法抛出异常之后执行;
  • @Around:环绕通知,围绕着方法执行。

示例项目新建:

第一步:新建spring aop项目

Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)

第二步:添加spring-aop.xml Spring配置文件:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.dx.spring.beans.aop"></context:component-scan>
    <!-- 配置是AspectJ注解起作用 :自动为匹配的类生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
View Code

相关文章: