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

AOP

概念

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

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.创建工程

1.1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.by</groupId>
    <artifactId>Spring_AOP_Xml</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Spring常用依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
         <!--支持切点表达式 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.8.RELEASE</version>
        </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
<?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"
       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">
        <!-- 用于配置前置通知:指定增强的方法在切入点方法之前执行 
    		method:用于指定通知类中的增强方法名称
    		ponitcut-ref:用于指定切入点
    	-->
    	<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实现接口时的类型

在这里插入图片描述

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

    在这里插入图片描述

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值