Spring AOP详解

AOP:Aspect Oriented Programing :面向切面编程
当需要为分散的对象引入公共的行为时,我们就会考虑以前所接触的封装、继承和多态性等来建立一种对象层次结构,用以模拟公共行为的一个集合还可行不,答案是显而易见的,不行。所以我们就需要用到AOP
AOP将其分为两个部分:核心关注点和横切关注点
核心关注点: 业务处理的主要流程
横切关注点:与业务处理的主要流程关系不大的部分。横切关注点的一个特点是,它们经常发生在核心关注点的多处,而各处都基本相似。比如权限认
证、日志、事务处理。
AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。
那除了核心业务想增加一些功能怎么办?
首先我们来看一下静态代理和动态代理
静态代理:
在这里插入图片描述
如上图所示:代理对象除了完成公共的业务操作以外,还可以进行一些日志的打印,安全性检查等业务。
接下来我们看一下动态代理:
在这里插入图片描述
每次JVM根据目标对象的不同需求给它生成不同的虚拟的代理对象去完成业务。
代码实现:

//用于生成动态代理对象(反射)
public class DynamicProxy implements InvocationHandler {
    //引入真实的对象
    public Object targetObject;
    //创建一个方法,通过反射生成动态代理对象
    public Object createDynamicProxyObjectInstance(Object paramObj) {
        this.targetObject = paramObj;
        return Proxy.newProxyInstance(
                this.targetObject.getClass().getClassLoader(),
                this.targetObject.getClass().getInterfaces(),
                this);
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      Object resultObj=null;
      try{
          checkSecurity();
          resultObj=method.invoke(this.targetObject,args);
          return resultObj;
      }catch(Exception e){
          e.printStackTrace();
      }
      return resultObj;
    }
    public void checkSecurity(){
        System.out.println("checkSecurity");
    }
}

测试类:

 public static void main(String[] args) {
        DynamicProxy dynamicProxy=new DynamicProxy();
        IUserManager iUserManager= (IUserManager) dynamicProxy.createDynamicProxyObjectInstance(new UserManagerImpl());
        iUserManager.addUser("Sally","888");
        iUserManager.modifyUser(1,"Sally","666");
        iUserManager.delUser(1);
    }

说明: IUserManager iUserManager= (IUserManager) dynamicProxy.createDynamicProxyObjectInstance(new UserManagerImpl());
每次根据需求创建动态代理对象。
比以上更好的解决方案就是由AOP提供的
使用AOP的优点:
降低模块耦合度
使系统容易扩展
延迟设计决定:使用AOP,设计师可以推迟为将来的需求作决定,因为需求作为独立的方面很容易实现
有更好的代码复用性
AOP术语:
通知(advice),切点(pointcut)和连接点(joinpoint)。
通知:描述了切面要完成的工作(有5种类型)
前置通知(Before): 在目标方法被调用之前调用通知功能;
后置通知(After): 在目标方法完成之后调用通知,此时不会关心方法的输出是什么;
返回通知(After-returning):在目标方法成功执行之后调用通知;
异常通知(After-throwing): 在目标方法抛出异常后通知调用;
环绕通知(Around):通知包裹了被通知的方法,在被通知的方法通知之前和调用之后执行自定义的行为
在这里插入图片描述
如上图所示:
Advice就是通知,增强处理的功能,比如说ChecckSecurity等,定义好,什么时候想用了再使用。允许Advice的地方,基本每个方法的before,after,throws都可以是JoinPoint(连接点),即和方法有关的前后都是连接点。若一个类中,有很多个方法,但是并不想都使用Advice,只是想让其中的几个,在调用方法before,after,throws时干点什么,就可以用PointCut来定义方法,筛选连接点,选中我们想要的 ,将其织入(weaving)到目标对象中.
通知和切入点的结合就是Aspect(切面)。
使用注解创建切面
(1)添加依赖
将以下的maven jar包添加到项目中去。

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

(2)定义切面:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class SecurityHandler {
    //PointCut标识一个范围
    @Pointcut("execution(* com.zh.*.*(..))")
private void allMethod(){}
//Advice[Before]
    //横切关注点
@Before("allMethod()")
public void checkSecurity(){
    System.out.println("=============checkSecurity()============");
}

}

(3)XML配置使用切面

<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">

    <!--?spring??????-->
    <aop:aspectj-autoproxy/>

    <bean id="securityHandler" class="com.zh.SecurityHandler"/>

    <bean id="userManagerImpl" class="com.zh.UserManagerImpl"/>
</beans>

切点表达式定义:
arg() : 限制连接点匹配参数为指定类型的执行方法
@args(): 限制连接点匹配参数由指定注解标注的执行方法
execution() : 用于匹配时连接点的执行方法
this() : 限制连接点匹配AOP代理的bean引用为指定类型的类
target : 限制连接点匹配目标对象为指定类型的类
@target : 限制连接点匹配特定的执行对象,这些对象对应的类要具有指定类型的注解
within() : 限制连接点匹配指定的类型
@within() : 限制连接点匹配指定注解所标注的类型(当使用Spring AOP时,方法定义在由指定的注解所标注
的类里)
@annotation() : 限定匹配带有指定注解的连接点

动态和静态代理的完整源代码请看github链接:
https://github.com/zhdfuture/Spring2/commit/1ca3145e2b48b27e858ff8831810c41c0c16bb27
AOP实现的完整源代码请看github链接:
https://github.com/zhdfuture/Spring3/commit/d5c81114a14465248cdc5aa595fd39e4daf15d89

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值