Spring学习之——AOP(面向切面)

AOP

概念

AOP:全称是Aspect Oriented Programming即:面向切面编程。
Spring学习之——AOP(面向切面)_第1张图片
简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对程序进行增强:权限校验,日志记录,性能监控,事务控制.

AOP相关术语

  1. 连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法。

    其实就是所有可能会被增强的方法

  2. 切入点(pointcut)(掌握)

    切入点是指我们要对哪些连接点进行拦截的定义

    就是我们选择部分所要增强的方法

  3. 通知(advice)(掌握)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

    (一般是用来扩展日志功能,或事务控制等)

  4. 切面(aspect)(掌握)

    是切入点和通知的结合

    就是讲增强(通知)应用到切入点的过程

  5. 引介(introduction)

    是一种特殊的通知,在不修改代码的前提下,引介可以在运行期为类动态地添加一些方法或字段

  6. 目标对象(Target)

    要代理的目标对象(要增强的类)

  7. 织入(weave)

    将增强应用到目标的过程将advice应用到target的过程

  8. 代理(Proxy)

    一个类被AOP织入增强之后,就产生一个代理类

Spring的AOP配置

1.创建工程

Spring学习之——AOP(面向切面)_第2张图片

1.1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.bygroupId>
    <artifactId>Spring_AOP_XmlartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.1.8.RELEASEversion>
        dependency>
         
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>5.1.8.RELEASEversion>
        dependency>
    dependencies>
project>
1.2.dao
/**
 * 持久层实现类
 */
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}
1.3.service
/**
 * 业务层实现类
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void addUser(){
        userDao.addUser();
    }
}
1.4.applicationContext.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/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="userDao" class="com.by.dao.UserDaoImpl">bean>
    <bean id="userService" class="com.by.service.UserServiceImpl">
        <property name="userDao" ref="userDao">property>
    bean>
beans>
1.5.web
/**
 * 模拟表现层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用对象
        UserService userService = ac.getBean("userService",UserService.class);
        System.out.println(userService.getClass());
        userService.addUser();
    }
}

2.增强

  1. 创建增强类

    package com.by.advice;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    import java.util.Date;
    
    public class MyLogAdvice {
    
        //前置通知
        public void before(){
            System.out.println("前置通知");
        }
    
        //后置通知【try】
        public void afterReturning(){
            System.out.println("后置通知");
        }
    
        //异常通知【catch】
        public void afterThrowing(){
            System.out.println("异常通知");
        }
    
        //最终通知【finally】
        public void after(){
            System.out.println("最终通知");
        }
    
        //环绕通知
        public void around(ProceedingJoinPoint joinPoint){
            try {
                System.out.println("方法执行前的环绕通知");
                joinPoint.proceed();
                System.out.println("方法执行后的环绕通知");
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
    
  2. 配置增强类

    
    <bean id="myLogger" class="com.by.advice.MyLogger">bean>
    

3.切点

  1. 切点表达式

    表达式语法:

    execution([修饰符] 返回值类型 包名.类名.方法名(参数))

    例如:

    execution(* com.by.service.UserService.add(..))

    execution(* com.by.service.UserService.*(..))

    execution(* com.by.service.*.*(..))

  2. 配置切点

    <aop:config>
        
        <aop:pointcut id="pointcut" expression="execution(* com.by.service.*.*(..))"/>
    aop:config>
    

4.切面

  1. 增强的类型

    • aop:before:用于配置前置通知
    • aop:after-returning:用于配置后置【try】通知,它和异常通知只能有一个执行
    • aop:after-throwing:用于配置异常【catch】通知,它和后置通知只能执行一个
    • aop:after:用于配置最终【finally】通知
    • aop:around:用于配置环绕通知
  2. 配置切面

    
    <aop:aspect ref="myLogger">
        
    	<aop:before method="before" pointcut-ref="pointcut"/>
        <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
        <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
        <aop:after method="after" pointcut-ref="pointcut"/>
        <aop:around method="around" pointcut-ref="pointcut"/>
    aop:aspect>
    

###5.测试

  1. 测试service实现接口时的类型

Spring学习之——AOP(面向切面)_第3张图片

  1. 测试service不实现接口时的类型

    Spring学习之——AOP(面向切面)_第4张图片

你可能感兴趣的:(Spring,spring,学习,java)