Spring AOP初步理解及使用(二)

简介: Spring AOP初步理解及使用

Spring AOP初步理解及使用(一)https://developer.aliyun.com/article/1426135


(3)切面类
package org.ymx.service;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
 * 注解@Aspect标识该类为切面类
 */
@Component
@Aspect
public class ServiceAspect {
    /**
     * 通过注解@Pointcut定义切点,stuSayHello()只是一个标识,无所谓是什么,
     * 方法中内容本身也是空的,使用该切点的地方直接通过标识stuSayHello()引用切点表达式。
     */
    @Pointcut("execution(* org.ymx.service.impl.StudentServiceImpl.sayHello(..))")
    public void stuSayHello() {
    }
    /**
     * 说话之前--张开嘴巴
     */
    @Before("stuSayHello()")
    public void openMouth() {
        System.out.println("Opens his mouth");
    }
    /**
     * 说话之前--睁开眼睛
     */
    @Before("stuSayHello()")
    public void openEyes() {
        System.out.println("Open one's eyes");
    }
    /**
     * 说话之后--微笑
     */
    @After("stuSayHello()")
    public void smile() {
        System.out.println("Smile");
    }
}

(4)AOP全局配置
package org.ymx.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
 * Jdk代理:基于接口的代理,一定是基于接口,会生成目标对象的接口的子对象。
 * Cglib代理:基于类的代理,不需要基于接口,会生成目标对象的子对象。
 *
 * 1. 注解@EnableAspectJAutoProxy开启代理;
 *
 * 2. 如果属性proxyTargetClass默认为false, 表示使用jdk动态代理织入增强;
 *
 * 3. 如果属性proxyTargetClass设置为true,表示使用Cglib动态代理技术织入增强;
 *
 * 4. 如果属性proxyTargetClass设置为false,但是目标类没有声明接口,
 *    Spring aop还是会使用Cglib动态代理,也就是说非接口的类要生成代理都用Cglib。
 */
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan("org.ymx.service")
public class AopConfig {
}
(5)测试
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.ymx.config.AopConfig;
import org.ymx.service.StudentService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AopConfig.class) //加载全局配置
public class test01 {
    @Autowired
    private StudentService studentService;
    @Test
    public void Test01(){
        studentService.sayHello();
    }
}

测试时一定要加载全局配置@ContextConfiguration(classes = AopConfig.class)不然AOP不起作用

3 实现方式二

与上述方式实现唯一不同点在于切面类使用的 @Around注解来替代@Before和@After

package org.ymx.service;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
 * 注解@Aspect标识该类为切面类
 */
@Component
@Aspect
public class ServiceAspect {
    /**
     * 通过注解@Pointcut定义切点,stuSayHello()只是一个标识,无所谓是什么,
     * 方法中内容本身也是空的,使用该切点的地方直接通过标识stuSayHello()引用切点表达式。
     */
    @Pointcut("execution(* org.ymx.service.impl.StudentServiceImpl.sayHello(..))")
    public void stuSayHello() {
    }
    @Around("stuSayHello()")
    public void testAround(ProceedingJoinPoint jp) {
        try {
            System.out.println("Opens his mouth");
            System.out.println("Open one's eyes");
            jp.proceed();//执行方法
            System.out.println("Smile");
        } catch (Throwable e) {
            System.out.println("没说出来");
        }
    }
}
4 测试结果

五、AOP实战(注解版)

1 简介

StudentService类具有SayHello()方法表示说Hello,但是在学生和别人说”Hello“之前,必须张开嘴巴和睁开眼睛,说完“Hello”之后再来一个回眸微笑,图示如下:

2 实现方式一
(1)pom依赖
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
(2)Service接口和实现类
## 接口:
package org.ymx.service;
public interface StudentService {
     void sayHello();
}
## 实现类:
package org.ymx.service.impl;
import org.springframework.stereotype.Service;
import org.ymx.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
    @Override
    public void sayHello() {
        System.out.println("Hello");
    }
} 
相关文章
|
4月前
|
监控 安全 Java
Spring AOP实现原理
本内容主要介绍了Spring AOP的核心概念、实现机制及代理生成流程。涵盖切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)等关键概念,解析了JDK动态代理与CGLIB代理的原理及对比,并深入探讨了通知执行链路和责任链模式的应用。同时,详细分析了AspectJ注解驱动的AOP解析过程,包括切面识别、切点表达式匹配及通知适配为Advice的机制,帮助理解Spring AOP的工作原理与实现细节。
|
26天前
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
|
25天前
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
|
8月前
|
XML Java 开发者
Spring Boot中的AOP实现
Spring AOP(面向切面编程)允许开发者在不修改原有业务逻辑的情况下增强功能,基于代理模式拦截和增强方法调用。Spring Boot通过集成Spring AOP和AspectJ简化了AOP的使用,只需添加依赖并定义切面类。关键概念包括切面、通知和切点。切面类使用`@Aspect`和`@Component`注解标注,通知定义切面行为,切点定义应用位置。Spring Boot自动检测并创建代理对象,支持JDK动态代理和CGLIB代理。通过源码分析可深入了解其实现细节,优化应用功能。
385 6
|
7月前
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
346 25
|
7月前
|
XML 安全 Java
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
251 24
|
6月前
|
Java API 微服务
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——Spring Boot 中的 AOP 处理
本文详细讲解了Spring Boot中的AOP(面向切面编程)处理方法。首先介绍如何引入AOP依赖,通过添加`spring-boot-starter-aop`实现。接着阐述了如何定义和实现AOP切面,包括常用注解如`@Aspect`、`@Pointcut`、`@Before`、`@After`、`@AfterReturning`和`@AfterThrowing`的使用场景与示例代码。通过这些注解,可以分别在方法执行前、后、返回时或抛出异常时插入自定义逻辑,从而实现功能增强或日志记录等操作。最后总结了AOP在实际项目中的重要作用,并提供了课程源码下载链接供进一步学习。
677 0
|
6月前
|
Java 开发者 微服务
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——什么是AOP
本文介绍了Spring Boot中的切面AOP处理。AOP(Aspect Oriented Programming)即面向切面编程,其核心思想是分离关注点。通过AOP,程序可以将与业务逻辑无关的代码(如日志记录、事务管理等)从主要逻辑中抽离,交由专门的“仆人”处理,从而让开发者专注于核心任务。这种机制实现了模块间的灵活组合,使程序结构更加可配置、可扩展。文中以生活化比喻生动阐释了AOP的工作原理及其优势。
290 0
|
8月前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
531 8
|
10月前
|
XML Java 数据安全/隐私保护
Spring Aop该如何使用
本文介绍了AOP(面向切面编程)的基本概念和术语,并通过具体业务场景演示了如何在Spring框架中使用Spring AOP。文章详细解释了切面、连接点、通知、切点等关键术语,并提供了完整的示例代码,帮助读者轻松理解和应用Spring AOP。
269 2
Spring Aop该如何使用