Spring面向切面编程(AOP)

转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/71747299,建议读者阅读原文,确保获得完整的信息

1.前言说明

  AOP即Aspect-Oriented Programming,面向切面的编程,与OOP想对应,是OOP的补充和完善,OOP横向上区分一个个类,AOP则从纵向上考察对象。AOP是一种动态地将代码切入到类的指定方法、指定位置上的编程思想。

  AOP一方面实现松耦合,使业务代码更加纯粹,即减少业务无关代码;另一方面实现统筹管理,提升业务开发效率,即可以随时给所有业务新增附加功能,而无须修改即有业务代码,这也体现了开闭原则,即对扩展开放,修改关闭。

2.应用场景

  • Log日志管理
  • Authentication 权限
  • Caching 缓存
  • Context passing 内容传递
  • Error handling 错误处理
  • Lazy loading 懒加载
  • Debugging 调试
  • Logging, tracing, profiling and monitoring 记录跟踪 优化 校准
  • Performance optimization 性能优化
  • Persistence 持久化
  • Resource pooling 资源池
  • Synchronization 同步
  • Transactions 事务

3.实例说明

  本文通过一个实例来说明SpringBoot中AOP的配置方式,至于其他的框架的配置方法,没有什么大差异,主要差一点会在依赖包上,Java方法类及AOP配置没有大差别。

  本文实例实现在指定包中的方法执行 前后执行切面方法,记录下方法输入、输出参数。

  第一步:pom.xml中添加依赖

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

  添加完上述依赖后,maven会自动下载依赖及间接依赖。如下图所示,SpringBoot AOP依赖包含3个依赖,spring-boot-starter-aop-1.3.6.RELEASE.jar、spring-sop-4.2.7.RELEASE.jar、aspectjweaver-1.8.9.jar。

这里写图片描述

  第二步:创建切面方法

public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    // 前置通知
    public void beforeAdvice(JoinPoint joinPoint){
        logger.info("============before advice");
        logger.info(joinPoint.getSignature().getDeclaringTypeName() + " [input parameters] " + Arrays.toString(joinPoint.getArgs()));
    }

    // 后置通知
    public void afterAdvice(JoinPoint joinPoint, Object retValue){
        logger.info("============after advice");
        logger.info(joinPoint.getSignature().getDeclaringTypeName() + " [output parameters] " + retValue);
    }

}

  上述类中,有两个方法,分别对应前置和后置两种模式执行的方法。前置方法实现输入参数打印,后置方法实现输出参数打印。

  第三步:新增AOP配置

  在已有的beans.xml文件中新增aop配置,由于原来的beans.xml不支持aop配置,因此需要对该文件做2点调整:

  • 新增头配置xmlns:aop、xsi:schemaLocation中关于aop的配置。
  • 添加aop切面及模式配置。

  本例中的配置仅截取关键配置项。

<?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-3.0.xsd">

    <bean id="logAspect" class="com.loongshawn.aop.LogAspect"/>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.loongshawn.method.ces..*.*(..))" />
        <aop:aspect ref="logAspect">
            <aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
            <aop:after-returning pointcut-ref="pointcut" arg-names="joinPoint,retValue" returning="retValue" method="afterAdvice"/>
        </aop:aspect>
    </aop:config>

</beans>

  配置参数解释说明:

  • aop.pointcut,横切点,即在什么位置执行切面方法。
  • aop.expression,横切点规则表达式,过滤符合规则的切点。有关表达式规则将会在另外一篇文章中详细说明。
  • aop.aspect,切面。
  • aop:before,前置模式,切面执行模式。
  • aop:after-returning,后置返回模式,切面执行模式。

4.运行结果

这里写图片描述

  程序正常运行,指定包中的方法在执行前后均会执行切面服务。本文仅对AOP进行了简要说明,更加详细深入的说明请参考网上的其他资料。

  由于刚开始梳理这块内容,知识面还不够宽,如果文中有错误之处,希望大家能够指出,谢谢!

5.参考资料

(1).什么是面向切面编程AOP:https://www.zhihu.com/question/24863332

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值