6.Aop操作(基于aspectj的xml方式)

  • aop基本概念
  • aop操作准备
  • 使用表达式配置切入点

aop操作基本概念

1 在spring里面进行aop操作,使用 aspectj(饿死怕死J)实现
(1)aspectj不是spring一部分,和spring一起使用进行aop操作
(2)Spring2.0以后新增了对AspectJ支持
2 使用aspectj实现aop有两种方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式

aop操作准备

导入Maven依赖和aop约束

<!-- ioc编程 -->
  <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>

使用表达式配置切入点

1 切入点:实际增强的方法
2 常用的表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* cn.itcast.aop.Book.add(..))
含义:任意修饰符,在cn.itcast.aop包下Book类中add方法(),有参数也包含
(2)execution(* cn.itcast.aop.Book.(..))
含义:任意修饰符,在cn.itcast.aop包下Book类中所有方法(),有参数也包含
(3)execution(
.(..))
含义:任意修饰符,在所有类下所有方法,有参数也包含
(4) 匹配所有save开头的方法 execution(* save*(..))
含义:任意修饰符,以save开头的任意方法都增强,有参数也包含

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

@Before:前置通知,在方法执行之前返回
@After:后置通知,在方法执行后执行
@AfterRunning:返回通知,在方法返回结果之后执行
@AfterThrowing:异常通知,在方法抛出异常之后
@Around:环绕通知,围绕着方法执行

package com.neuedu.aop;

public class Book {

    public void add() {
        System.out.println("add...........");
    }
}
package com.neuedu.aop;

public class MyBook {

    public void before1() {
        System.out.println("前置增强......");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 1.配置对象 -->
<bean id="book" class="com.neuedu.aop.Book"></bean>
<bean id="myBook" class="com.neuedu.aop.MyBook"></bean>

<!-- 2.配置Aop操作 -->
<aop:config>
    <!-- 2.1配置切入点 -->
    <aop:pointcut expression="execution(* com.neuedu.aop.Book.*(..))" id="qieru"/>
    <!-- 2.2配置切面,把增强用到方法上面 -->
        <aop:aspect ref="myBook">
        <!-- 2.3配置增强类型method:增强类里面使用哪个方法作为前置-->
            <aop:before method="before1" pointcut-ref="qieru"/>
       </aop:aspect>
</aop:config>
</beans>
举例

测试

其他Aspectj的aop其他操作

package com.neuedu.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

    public void before1() {
        System.out.println("前置增强......");
    }
    
    public void after1() {
        System.out.println("后置增强......");
    }
    
    //环绕通知
    public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        //方法之前
        System.out.println("方法之前...");
        
        //执行被增强的方法
        proceedingJoinPoint.proceed();
        
        //方法之后
        System.out.println("方法之后...");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 1.配置对象 -->
<bean id="book" class="com.neuedu.aop.Book"></bean>
<bean id="myBook" class="com.neuedu.aop.MyBook"></bean>
<!-- 2.配置Aop操作 -->
<aop:config>
    <!-- 2.1配置切入点 -->
    <aop:pointcut expression="execution(* com.neuedu.aop.Book.*(..))" id="qieru"/>
    <!-- 2.2配置切面,把增强用到方法上面 -->
        <aop:aspect ref="myBook">
        <!-- 2.3配置增强类型method:增强类里面使用哪个方法作为前置-->
            <aop:before method="before1" pointcut-ref="qieru"/>
            <!-- 后置增强 -->
            <aop:after-returning method="after1" pointcut-ref="qieru"/>
            <!-- 环绕通知 -->
            <aop:around method="around1" pointcut-ref="qieru"/>
       </aop:aspect>
</aop:config>
</beans>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容