SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(四)

简介: SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(四)

⑥. XML配置AOP详解


  • ①. 切点表达式的写法


execution([修饰符] 返回值类型 包名.类名.方法名(参数))
访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号*  代表任意
包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
execution(public void com.itheima.aop.Target.method())  
execution(void com.itheima.aop.Target.*(..))
// 常用:aop包下任意类的任意方法
execution(* com.itheima.aop.*.*(..))
//aop包及其子类任意类的任意方法
execution(* com.itheima.aop..*.*(..))
execution(* *..*.*(..))


②. 通知的类型


<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>


微信图片_20220108213925.png


③. 代码展示


public class MyAspect {
    /*前置通知*/
    public void before(){
        System.out.println("前置增强");
    }
    /*后置通知*/
    public void afterRetruning(){
        System.out.println("后置通知");
    }
    /*环绕通知
    ProceedingJoinPoint:正在执行的连接点[切点]
     */
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");
        //切点方法
        Object proceed=pjp.proceed();
        System.out.println("环绕后增强...");
        return proceed;
    }
    /*异常通知*/
    public void afterThrowing(){
        System.out.println("异常抛出异常");
    }
    /*最终通知*/
    public void after(){
        System.out.println("最终通知执行了");
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--目标对象-->
    <bean id="target" class="com.xiaozhi.aop.Target"/>
    <!--切面对象-->
    <bean id="myAspect" class="com.xiaozhi.aop.MyAspect"></bean>
    <!--配置织入:告诉Spring框架,哪些方法(切点)需要进行哪些增强(前置 | 后置)-->
    <aop:config>
          <!--声明切面-->
          <aop:aspect ref="myAspect">
          <!--切面:切入点+通知-->
              <!--通知
              method:切面类中增强的方法
              pointcut:切入点表达式
              -->
              <!--前置通知-->
              <aop:before method="before" pointcut="execution(public void com.xiaozhi.aop.Target.save())"></aop:before>
              <!--后置通知-->
              <aop:after-returning method="afterRetruning" pointcut="execution(* com.xiaozhi.aop.*.*(..))"></aop:after-returning>
              <!--环绕通知-->
              <aop:around method="around" pointcut="execution(* com.xiaozhi.aop.*.*(..))"></aop:around>
              <!--异常通知-->
              <aop:after-throwing method="afterThrowing" pointcut="execution(* com.xiaozhi.aop.*.*(..))"></aop:after-throwing>
              <!--最终通知-->
              <aop:after method="after" pointcut="execution(* com.xiaozhi.aop.*.*(..))"></aop:after>
          </aop:aspect>
    </aop:config>
</beans>


④. 切点表达式的抽取


(当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式)


<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <aop:pointcut id="myPointcut" expression="execution(* com.xiaozhi.aop.*.*(..))"/>
        <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
    </aop:aspect>
</aop:config>



相关文章
|
12天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
15 0
|
5天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
13 0
|
5天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0
|
5天前
|
安全 Java API
第1章 Spring Security 概述(2024 最新版)(下)
第1章 Spring Security 概述(2024 最新版)
16 0
|
5天前
|
安全 Java 数据安全/隐私保护
第1章 Spring Security 概述(2024 最新版)(上)
第1章 Spring Security 概述(2024 最新版)
23 0
|
6天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
18 0
|
6天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
12天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
21 1
|
12天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
19 0
|
12天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
24 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析