ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Spring】学习笔记09-AOP

2022-06-21 22:34:46  阅读:257  来源: 互联网

标签:Spring void 09 OOP 关注点 AOP 横切 public


什么是AOP?

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分
进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP和OOP的关系

区分

AOP、OOP在字面上虽然非常类似,但却是面向不同领域的两种设计思想。OOP(面向对象编程)针对业务处理过程的实体及其属性和行为进行抽象封装,以获得更加清晰高效的逻辑单元划分。 而AOP则是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。这两种设计思想在目标上有着本质的差异。 上面的陈述可能过于理论化,举个简单的例子,对于“雇员”这样一个业务实体进行封装,自然是OOP/OOD的任务,我们可以为其建立一个“Employee”类,并将“雇员”相关的属性和行为封装其中。 而用AOP设计思想对“雇员”进行封装将无从谈起。 同样,对于“权限检查”这一动作片断进行划分,则是AOP的目标领域。而通过OOD/OOP对一个动作进行封装,则有点不伦不类。 换而言之,OOD/OOP面向名词领域,AOP面向动词领域。

关系

很多人在初次接触 AOP 的时候可能会说,AOP 能做到的,一个定义良好的 OOP 的接口也一样能够做到,我想这个观点是值得商榷的。AOP和定义良好的 OOP 的接口可以说都是用来解决并且实现 需求中的横切问题的方法。但是对于 OOP 中的接口来说,它仍然需要我们在相应的模块中去调用该接口中相关的方法,这是 OOP 所无法避免的,并且一旦接口不得不进行修改的时候,所有事情 会变得一团糟;AOP 则不会这样,你只需要修改相应的 Aspect,再重新编织(weave)即可。 当然,AOP 也绝对不会代替 OOP。核心的需求仍然会由 OOP 来加以实现,而 AOP 将会和 OOP 整合 起来,以此之长,补彼之短。

AOP名词分析

  • 横切关注点:跨越多个跨越多个模块的非业务核心功能.(类似于:授权,日志,错误检测等)横切关注点经常发生在核心关注点周围,AOP的作用就是分离系统中的各种关注点,将核心关注点和横切关注点分离开
  • 切面(Aspect):对横切关注点的抽象,可以理解为横切关注点的类
  • 连接点(join point):程序执行中的一个执行点,可以是一个类的具体方法,连接点 是一个抽象概念,在AOP中,不需要可以去创建要给连接点
  • 切入点(point cut):本质上是一个捕获连接点的结果.在AOP中,可以定义一个切入点,捕获相关方法的调用
  • 引入(introd

     

    uce):为对象(可以是方法或者类对象)添加额外的方法或者属性,达到改变对象结构的目的。

 

 

 

核心关注点和横切关注点的分离

一个关注点(concern)就是一个特定的目的,一块我们感兴趣的区域,一段我们需要的逻辑行为。从技术的角度来说,一个典型的软件系统包含一些核心的关注点和系统级的关注点。举个例子来说,一个信用卡处理系统的核心关注点是借贷/存入处理,而系统级的关注点则是日志

、事务完整性、授权、安全及性能问题等,许多关注点——即横切关注点(crosscutting concerns)——会在多个模块中出现。如果使用现有的编程方法,横切关注点会横越多个模块,结果是使系统难以设计、理解、实现和演进。AOP能够比上述方法更好地分离系统关注点,从而

提供模块化的横切关注点。

例如一个复杂的系统,它由许多关注点组合实现,如业务逻辑、性能,数据存储、日志和调度信息、授权、安全、线程、错误检查等,还有开发过程中的关注点,如易懂、易维护、易追查、易扩展等,图2.1演示了由不同模块实现的一批关注点组成一个系统。

通过对系统需求和实现的识别,我们可以将模块中的这些关注点分为:核心关注点和横切关注点。对于核心关注点而言,通常来说,实现这些关注点的模块是相互独立的,他们分别完成了系统需要的商业逻辑,这些逻辑与具体的业务需求有关。而对于日志、安全、持久化等关注点而言 ,他们却是商业逻辑模块所共同需要的,这些逻辑分布于核心关注点的各处。在AOP中,诸如这些模块,都称为横切关注点。应用AOP的横切技术,关键就是要实现对关注点的识别。 通过利用AOP技术,改变了整个系统的设计方式。在分析系统需求之初,利用AOP的思想,分离出核心关注点和横切关注点。在实现了诸如日志、事务管理、权限控制等横切关注点的通用逻辑后,开发人员就可以专注于核心关注点,将精力投入到解决企业的商业逻辑上来。同时,这 些封装好了的横切关注点提供的功能,可以最大限度地复用于商业逻辑的各个部分,既不需要开发人员作特殊的编码,也不会因为修改横切关注点的功能而影响具体的业务功能。  

使用Spring的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> </beans>

 

接口及实现类 UserService接口  
package com.wang.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

UserService接口实现类

package com.wang.service;

public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void select() {
        System.out.println("选择了一个用户");
    }
}

 日志切面类

package com.wang.log;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.aspectj.AspectJAfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(o1.getClass().getName()+"方法,返回结果为"+o);
    }
}

AOP实现方式1:使用Spring的API接口

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="userService" class="com.wang.service.UserServiceImpl" />
    <bean id="log" class="com.wang.log.Log"/>
    <bean id="afterlog" class="com.wang.log.AfterLog"/>
<!-- 配置aop:需要导入aop的约束,首先需要一个切入点-->
<!--    方式一:使用Spring原生的API接口-->
<!--  切入点:  expression:表达式,execution(要执行的位置!*****)     -->
    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut1"/>
<!--        上述一行配置意思是:将log类切入到切入点 pointcut1-->
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut1"/>
    </aop:config>

</beans>

 

AOP实现方式2:自定义类

package com.wang.diy;

public class DiyPointCut {
    public void before(){
        System.out.println("======方法执行前=======");

    }
    public void after(){
        System.out.println("=====方法执行后=====");
    }
}

 

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="userService" class="com.wang.service.UserServiceImpl" />
    <bean id="log" class="com.wang.log.Log"/>
    <bean id="afterlog" class="com.wang.log.AfterLog"/>

    <bean id="diy" class="com.wang.diy.DiyPointCut">
    </bean>
    <aop:config>
<!--        自定义切面Ref要引用的类bean,-->
        <aop:aspect ref="diy">
            <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after-returning method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

</beans>

AOP实现方式3:使用注解实现AOP

定义切面类,并在切面类中定义切入点

package com.wang.diy;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPoint {
    @Before("execution(* com.wang.service.UserService.*(..))")
    public void Before(){
        System.out.println("方法执行前");
    }
    @After("execution(* com.wang.service.UserService.*(..))")
    public void After(){
        System.out.println("方法执行后");
    }
    @Around("execution(* com.wang.service.UserService.*(..))")
    public void Around(ProceedingJoinPoint jp){
        System.out.println("环绕前");
        //执行方法
        try {
            Object proceed = jp.proceed();
            System.out.println(jp.getSignature());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            System.out.println("环绕后");
        }

    }
}

 

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="userService" class="com.wang.service.UserServiceImpl" />
    <bean id="log" class="com.wang.log.Log"/>
    <bean id="afterlog" class="com.wang.log.AfterLog"/>

<!--    通过注解实现Aop-->
    <bean id="annotationpint" class="com.wang.diy.AnnotationPoint">

    </bean>
<!--    开启注解支持  JDK(默认)
proxy-target-class :false 使用jdk实现动态代理
                   :true  使用cglib实现动态代理
-->
    <aop:aspectj-autoproxy proxy-target-class="false"/>
</beans>

 

AOP默认实现通过jdk实现动态代理,我们可以通过配置aop:aspectj-autoproxy 来选择通过jdk或者cglib来实现AOP

关于AOP相关博客链接

标签:Spring,void,09,OOP,关注点,AOP,横切,public
来源: https://www.cnblogs.com/WangGuangYuan/p/16398313.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有