Spring面向切面编程(AOP)

栏目: 编程工具 · 发布时间: 8年前

内容简介:Spring面向切面编程(AOP)

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>

第二步:创建切面方法

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配置

<?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.运行结果

Spring面向切面编程(AOP)

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

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

5.参考资料

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Blog Design Solutions

Blog Design Solutions

Richard Rutter、Andy Budd、Simon Collison、Chris J Davis、Michael Heilemann、Phil Sherry、David Powers、John Oxton / friendsofED / 2006-2-16 / USD 39.99

Blogging has moved rapidly from being a craze to become a core feature of the Internetfrom individuals sharing their thoughts with the world via online diaries, through fans talking about their favori......一起来看看 《Blog Design Solutions》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具