Spring框架学习笔记05:Spring AOP基础

目录

一、Spring AOP概述

1、AOP含义

AOP: Aspect-Oriented Programming (面向切面编程)

2、AOP作用

Spring的AOP作用在于解耦。AOP让一组类共享相同的行为(比如事务管理、日志管理、安全管理)。OOP(Object-Oriented Programming)只能通过继承类或实现接口来增加代码的耦合度,而且类继承是单根继承(不允许一子多父),阻碍了将更多的行为添加到一组类上,此时AOP可以弥补OOP的不足。

3、AOP与OOP

  • AOP(Aspect-Oriented Programming)—— 横向的关系
  • OOP(Object-Oriented Programming)—— 纵向的关系

4、AOP使用方式

Spring里有两种方式使用AOP:(1)配置方式;(2)注解方式

二、采用配置方式使用AOP

1、在net.zy.spring包里创建lesson05.aop_xml子包

在这里插入图片描述

2、在aop_xml子包里创建杀龙任务类 - SlayDragonQuest

@Component
public class SlayDragonQuest {
    public void embark(){
        System.out.println("执行杀龙任务");
    }
}

3、在aop_xml子包里创建勇敢骑士类 - BraveKnight

@Component
public class BraveKnight {
    @Autowired
    private SlayDragonQuest slayDragonQuest;
    public void embarkOnQuest(){
        slayDragonQuest.embark();
    }
}

4、在aop_xml子包里创建游吟诗人类 - Minstrel

@Component
public class Minstrel {
    public void singBeforeQuest(){
        System.out.println("啦啦啦,骑士出发了!");
    }
    public void singAfterQuest(){
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

5、创建Spring配置文件

  • 在resources里创建aop_xml目录,在里面创建spring-config.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:context="http://www.springframework.org/schema/context"
       xmlns:app="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/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="net.zy.spring.lesson05.aop_xml"/>
    <app:config>
        <app:aspect ref="minstrel">
            <app:pointcut id="embark" expression="execution(* net.zy.spring.lesson05..*.embarkOnQuest(..))"/>
            <app:before method="singBeforeQuest" pointcut-ref="embark"/>
            <app:after method="singAfterQuest" pointcut-ref="embark"/>
        </app:aspect>
    </app:config>

</beans>

(1)切点

在使用Spring框架配置AOP时,不管是通过XML配置文件还是注解方式,都需要定义pointcut(切点)。

(2)切点表达式

定义切点表达式execution (* net.hw.spring….(…))

(3)切换函数

execution()是最常用的切点函数,整个表达式可以分为五个部分。

  • execution():表达式主体。

  • 第一个*号:表示返回类型,*号表示所有的类型。

  • 包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,net.hw.spring包、子孙包下所有类的方法。

  • 第二个*号:表示类名,*号表示所有的类。

  • *(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

6、创建测试类- TestKnight

在这里插入图片描述

public class TestKnight {
    private ClassPathXmlApplicationContext context;

    @Before
    public void init() {
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
    }

    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器里获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("braveKnight");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
        // 关闭应用容器
        context.close();
    }
}

7、在pom.xml文件里添加AOP相关依赖

<!--Spring AOP-->                                          
<dependency>                                               
    <groupId>org.springframework</groupId>                 
    <artifactId>spring-aop</artifactId>                    
    <version>${spring.version}</version>                   
</dependency>                                              
<!--AspectJ支持-->                                           
<dependency>                                               
    <groupId>aspectj</groupId>                             
    <artifactId>aspectjrt</artifactId>                     
    <version>1.5.4</version>                               
</dependency>                                              
<dependency>                                               
    <groupId>org.aspectj</groupId>                         
    <artifactId>aspectjweaver</artifactId>                 
    <version>1.9.6</version>                               
    <scope>runtime</scope>                                 
</dependency>                                              

8、运行测试方法testBraveKnight(),查看结果

在这里插入图片描述

三、采用注解方式使用AOP

1、在net.zy.spring包里创建lesson05.aop_annotation子包

在这里插入图片描述

2、在aop_annotation子包里创建杀龙任务类 - SlayDragonQuest

@Component
public class SlayDragonQuest {
    public void embark(){
        System.out.println("执行杀龙任务。");
    }
}

3、在aop_annotation子包里创建勇敢骑士类 - BraveKnight

@Component
public class BraveKnight {
    @Autowired
    private SlayDragonQuest slayDragonQuest;
    public void  embarkOnQuest(){
        slayDragonQuest.embark();
    }
}

4、在aop_annotation子包里创建注解接口 - Action

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String name();
}
  • (1)@Target({ElementType.TYPE}) 注解 ElementType
    这个枚举类型的常量提供了一个简单的分类:注解可能出现在Java程序中的语法位置(这些常量与元注解类型(@Target)一起指定在何处写入注解的合法位置)
  • (2)@Retention({RetentionPolicy.Runtime}) 注解
    RetentionPolicy这个枚举类型的常量描述保留注解的各种策略,它们与元注解(@Retention)一起指定注释要保留多长时间
  • (3)@Documented注解 Documented注解表明这个注解是由 javadoc记录的,在默认情况下也有类似的记录工具。
    如果一个类型声明被注解了文档化,它的注解成为公共API的一部分。

5、在aop_annotation子包里创建游吟诗人切面 - MinstrelAspect

@Aspect // 声明切面
@Component // 声明为Spring管理的Bean
public class MinstrelAspect {
    // 注解声明切点
    @Pointcut("execution(* *.embarkOnQuest(..))")
    public void embark() {
    }

    // 注解声明前置通知
    @Before("embark()")
    public void singBeforeQuest(JoinPoint joinPoint) {
        System.out.println("啦啦啦,骑士出发了!");
    }

    // 注解声明后置通知
    @After("embark()")
    public void singAfterQuest(JoinPoint joinPoint) {
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

在这里插入图片描述

6、在aop_annotation子包里创建Spring配置类 - AopConfig

@Configuration // 标明是Spring配置类
@ComponentScan("net.zy.spring.lesson05.aop_annotation") // 组件扫描
@EnableAspectJAutoProxy // 开启Spring对ApectJ的支持
public class AopConfig {
}

7、在aop_annotation子包里创建测试类 - TestKnight

在这里插入图片描述

8、运行测试方法testBraveKnight(),查看效果

在这里插入图片描述

9、增加拯救少女任务类与拯救少女骑士类

@Component
public class SlayDragonQuest {
    public void embark(){
        System.out.println("执行杀龙任务。");
    }
}

@Component
public class DamselRescuingKnight {
    @Autowired
    private RescueDamselQuest rescueDamselQuest;
    public void  embarkOnQuest(){
        rescueDamselQuest.embark();
    }
}
  • 在测试程序里增加对拯救少女骑士的测试方法 - testDamselRescuingKnight()
    @Test
    public void testDamselRescuingKnight() {
        //根据名称从应用容器里获取救美骑士对象
        DamselRescuingKnight damselRescuingKnight = (DamselRescuingKnight) context.getBean("damselRescuingKnight");
        //救美骑士执行任务
        damselRescuingKnight.embarkOnQuest();
    }

在这里插入图片描述

10、实现注解式拦截

(1)修改勇敢骑士类,给embarkOnQuest()添加自定义注解Action

在这里插入图片描述

(2)修改游吟诗人切面类 - MinstrelAspect

@Aspect // 声明切面
@Component // 声明为Spring管理的Bean
public class MinstrelAspect {
    // 注解声明切点
/*    @Pointcut("execution(* *.embarkOnQuest(..))")*/
    @Pointcut("@annotation(net.zy.spring.lesson05.aop_annotation.Action)")
    public void embark() {
    }

    // 注解声明前置通知
    @Before("embark()")
    public void singBeforeQuest(JoinPoint joinPoint) {
        //获取方法签名
        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
        //获取被拦截的方法
        Method method=signature.getMethod();
        //获取注解式拦截
        Action action=method.getAnnotation(Action.class);
        //提示用户被拦截了
        System.out.println("["+action.name()+"]"+"拦截了"+method.getName()+":拦截前!");
        System.out.println("啦啦啦,骑士出发了!");
    }

    // 注解声明后置通知
    @After("embark()")
    public void singAfterQuest(JoinPoint joinPoint) {
        //获取方法签名
        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
        //获取被拦截的方法
        Method method=signature.getMethod();
        //获取注解式拦截
        Action action=method.getAnnotation(Action.class);
        //提示用户被拦截了
        System.out.println("["+action.name()+"]"+"拦截了"+method.getName()+":拦截后!");
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

(3)运行测试方法testBraveKnight(),查看效果

在这里插入图片描述

(4)修改拯救少女骑士类,给embarkOnQuest()添加自定义注解Action

在这里插入图片描述

(5)运行测试方法testDamselRescuingKnight(),查看效果

在这里插入图片描述

四:输出骑士完成任务的耗时

  • 创建耗时切面类 - ElapseAspect
  • 在这里插入图片描述
@Aspect // 声明切面
@Component // 声明为Spring管理的Bean
public class ElapseAspect {

  private static final long ONE_MINUTE = 60000;
    @Pointcut("@annotation(net.zy.spring.lesson05.aop_annotation.Action)")
    public void embark() {
    }
    @Around("embark()")
    public Object timeAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();
        long startTime = System.currentTimeMillis();

        obj = joinPoint.proceed(args);



        // 获取执行的方法名
        long endTime = System.currentTimeMillis();
        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();

        // 打印耗时的信息
        this.printExecTime(methodName, startTime, endTime);

        return obj;
    }

    /**
     * 打印方法执行耗时的信息,如果超过了一定的时间,才打印
     * @param methodName
     * @param startTime
     * @param endTime
     */
    private void printExecTime(String methodName, long startTime, long endTime) {
        long diffTime = endTime - startTime;
        System.out.println(" 任务耗时:" + diffTime + " ms");
        
    }


}

  • 运行测试方法TestKnight()查看效果
  • 在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值