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

相关文章

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu