Spring之AOP面向切面编程实现(一)

2024-09-06 16:38

本文主要是介绍Spring之AOP面向切面编程实现(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现方式:基于配置XML和基于注解实现。


场景:一个手机进货系统,一旦要进货(或出货),要提前记录进货时间,进货完毕后,还要提醒其它人进行验货。

分析:3步走,1,操作进货(或出货)的方法的时候,先记录当前的时间,完毕后,提醒其他人验货。为了不使代码变得冗余,采用aop的策略实现。

基于配置实现

进货出货的接口:

IPhoneService.java

public interface IPhoneService {public void salePhone();public void buyPhone();
}

进货出货的接口实现:

PhoneServiceImpl.java

public class PhoneServiceImpl implements IPhoneService{@Overridepublic void salePhone() {System.out.println("销售iPhone系列手机");}@Overridepublic void buyPhone() {System.out.println("进货iPhone系列手机");//throw new RuntimeException();//模拟出现异常使用}}

切入的事件处理:

LogAdvice.java

import org.aspectj.lang.JoinPoint;public class LogAdvice {public void before(JoinPoint jp){System.out.println("1.在" + jp.getSignature().getName() + "执行之前切入的内容"+",记录进出货时间");}public void afterMethod(JoinPoint jp){System.out.println("2.在" + jp.getSignature().getName() + "执行之后切入的内容"+",提醒其他人验货");}public void afterThrow(JoinPoint jp,RuntimeException re){System.out.println("3.方法" + jp.getSignature().getName() + "执行过程中,抛出了异常");}}

spring配置(切面,切点,切入事件)

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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="phoneService" class="com.ysdit.spring2.service.PhoneServiceImpl"></bean><!-- 增强处理,切入的内容 --><bean id="logAdvice" class="com.ysdit.spring2.service.aop.LogAdvice"></bean><aop:config><!-- 地点,切入的位置,该包下的任何方法的任何返回值,任何参数。都切入 --><aop:pointcut expression="execution(* com.ysdit.spring2.service.*.*(..))" id="phonePoint"/><aop:aspect ref="logAdvice" id="ap1"><!-- 时间,非功能需求代码切入的时间 --><aop:after-returning method="afterMethod" pointcut-ref="phonePoint"/></aop:aspect><aop:aspect ref="logAdvice" id="ap2"><aop:before method="before" pointcut-ref="phonePoint"/></aop:aspect><aop:aspect ref="logAdvice" id="ap3"><aop:after-throwing method="afterThrow" pointcut-ref="phonePoint" throwing="re"/></aop:aspect></aop:config></beans>

测试类:

AopTest.java

import com.ysdit.spring2.service.IPhoneService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {/*** @param args*/public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");IPhoneService phoneService = (IPhoneService) ctx.getBean("phoneService");phoneService.buyPhone();}}

执行购手机的方法,控制台打印如下:

这里写图片描述

执行卖手机的方法,控制台打印如下:

这里写图片描述

模拟执行卖手机的方法,出现异常,控制台打印如下:

这里写图片描述

发生异常,程序依旧能通知我们出现了异常,能通知及时处理.

百度云云盘分享(配置实现):http://pan.baidu.com/s/1dFG46w5


基于注解实现

修改:
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!--告诉spring要使用aop注解 --><aop:aspectj-autoproxy/><bean id="phoneService" class="com.ysdit.spring2.service.PhoneServiceImpl"></bean><!-- 增强处理,切入的内容 --><bean id="logAdvice" class="com.ysdit.spring2.service.aop.LogAdvice"></bean></beans>

修改:
LogAdvice.java

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;@Aspect
public class LogAdvice {@Pointcut("execution(* com.ysdit.spring2.service.*.*(..))")//这样是为了,实现切入点的重用public void pointcut(){}@Before("pointcut()")public void before(JoinPoint jp){System.out.println("1.在" + jp.getSignature().getName() + "执行之前切入的内容"+",记录进出货时间");}@After("pointcut()")public void afterMethod(JoinPoint jp){System.out.println("2.在" + jp.getSignature().getName() + "执行之后切入的内容"+",提醒其他人验货");}@AfterThrowing("pointcut()")public void afterThrow(JoinPoint jp){System.out.println("3.方法" + jp.getSignature().getName() + "执行过程中,抛出了异常");}}

执行购手机的方法,控制台打印如下:

这里写图片描述

执行卖手机的方法,控制台打印如下:

这里写图片描述

模拟执行卖手机的方法,出现异常,控制台打印如下:

这里写图片描述

发生异常,程序依旧能通知我们出现了异常,能通知及时处理.

百度云盘链接(注解实现):http://pan.baidu.com/s/1nu5OC8h


对于spring-boot 启用aop注解

1.添加依赖

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.开启注解

spring.aop.auto=true

完成

这篇关于Spring之AOP面向切面编程实现(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142553

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.