Spring---AoP,面向切面编程)原理学习笔记【全】

技术Spring---AoP,面向切面编程)原理学习笔记【全】 Spring---AoP(面向切面编程)原理学习笔记【全】1、AOP
1.1、什么是AOP
AOP(Aspect Oriented Pro

Spring - AoP(面向方面编程)原理学习笔记[完整]。

1、AOP

1.1、什么是AOP

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

1.2、Spring中的AOP应用

跨应用程序多个模块的方法或功能。也就是说,与我们的业务逻辑无关,但是我们需要注意的部分,就是横切关注点。如日志、安全、缓存、事务等。

方面:横切关注点被模块化的特殊对象。也就是说,它是一个类。

建议:必须由部门完成的工作。也就是说,它是类中的一个方法。

目标:通知的对象。

代理:向目标对象应用通知后创建的对象。

JointPoint:与切入点匹配的执行点。

1.3、在Spring中使用AOP

[导入依赖包]

!https://mvnrepository.com/artifact/org.aspectj/aspectjweaver

属国

GroupIdog . AspectJ/GroupId

artifactIdaspectjweaver/artifactId

版本1 . 9 . 4/版本

/依赖性

1.3.1、通过 Spring API 实现

业务界面:

公共接口用户服务{

public void add();

public void delete();

public void update();

public void search();

}

实现类:

公共类UserServiceImpl实现了用户服务{

@覆盖

public void add(){ 0

System.out.println('添加用户');

}

@覆盖

public void delete(){ 0

System.out.println('删除用户');

}

@覆盖

public void update(){ 0

System.out.println('更新用户');

}

@覆盖

public void search(){ 0

System.out.println('查询用户');

}

}

增强前课程和增强后课程:

公共类日志实现了MethodBeforeAdvice {

//方法:要执行的目标对象的方法。

//对象:被调用方法的参数。

//对象:目标对象。

@覆盖

在(方法方法,对象[]对象,对象o)引发Throwable之前的公共void

系统。out.println()是o.getclass()的“method.getName()”方法。getname()“已执行”);

}

}

公共类AfterLog实现AfterReturningAdvice {

//returnValue返回该值。

//方法被调用的方法。

//被调用方法的对象的args参数。

//以被调用的目标对象为目标。

@覆盖

公共void afterReturning(对象返回值,方法方法,对象[]参数,对象目标)引发Throwable {

system . out . println(' executed ' target . getclass()。getName()。

“.”的“method.getName()”方法

返回值:“ret”。

urnValue);
}
}

spring的xml中注册 , 并实现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.xsd"
    !--注册bean--
    bean  class="com.kuang.service.UserServiceImpl"/
    bean  class="com.kuang.log.Log"/
    bean  class="com.kuang.log.AfterLog"/
    !--aop的配置--
    aop:config
        !--切入点  expression:表达式匹配要执行的方法--
        aop:pointcut  expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/
        !--执行环绕; advice-ref执行方法 . pointcut-ref切入点--
        aop:advisor advice-ref="log" pointcut-ref="pointcut"/
        aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/
    /aop:config
/beans

测试:

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.search();
    }
}

Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 将会把公共业务加进来 . 实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 其本质还是动态代理!

1.3.2、自定义类来实现Aop

目标业务类是userServiceImpl

一、写一个切入类

public class DiyPointcut {
    public void before(){
        System.out.println("---------方法执行前---------");
    }
    public void after(){
        System.out.println("---------方法执行后---------");
    }
}

二、spring配置

!--第二种方式自定义实现--
!--注册bean--
bean  class="com.kuang.config.DiyPointcut"/
!--aop的配置--
aop:config
    !--第二种方式:使用AOP的标签实现--
    aop:aspect ref="diy"
        aop:pointcut  expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/
        aop:before pointcut-ref="diyPonitcut" method="before"/
        aop:after pointcut-ref="diyPonitcut" method="after"/
    /aop:aspect
/aop:config

三、测试

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}
1.3.3、注解实现

第一步:编写一个注解实现的增强类

package com.kuang.config;
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 AnnotationPointcut {
    @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("---------方法执行前---------");
    }
    @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("---------方法执行后---------");
    }
    @Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        System.out.println("签名:"+jp.getSignature());
        //执行目标方法proceed
        Object proceed = jp.proceed();
        System.out.println("环绕后");
        System.out.println(proceed);
    }
}

第二步:在Spring配置文件中,注册bean,并增加支持注解的配置

!--第三种方式:注解实现--
bean  class="com.kuang.config.AnnotationPointcut"/
aop:aspectj-autoproxy/

aop:aspectj-autoproxy:说明

通过aop命名空间的aop:aspectj-autoproxy /声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被aop:aspectj-autoproxy /隐藏起来了
aop:aspectj-autoproxy /有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为aop:aspectj-autoproxy poxy-target-class="true"/时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

【参考】https://www.kuangstudy.com/zl/ssm#1381804434102726657

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/62050.html

(0)

相关推荐

  • 闯红灯12123多久能显示,闯红灯多久能查到具体车辆

    技术闯红灯12123多久能显示,闯红灯多久能查到具体车辆随着互联网飞速发展,车辆在违章之后,既可到车管所进行违章处理,也可在网上进行处理闯红灯12123多久能显示。那么2018年闯红灯后几天后才能知晓呢?怎么查询呢?接下

    生活 2021年10月31日
  • 什么是javascript超集

    技术什么是javascript超集本篇内容介绍了“什么是javascript超集”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有

    攻略 2021年10月25日
  • 春节放假几天,今年春节放假有什么安排

    技术春节放假几天,今年春节放假有什么安排谢谢邀请春节放假几天,先前安排好的节日出行在防疫要求下让道,但是我也要说出来和大家分享一下不能实现的安排。1、86岁的母亲告诉我,没有坐过白天的飞机,言下之意想坐一次!基于这个想法

    生活 2021年10月27日
  • 解除流量封顶,40G流量用完啦,怎么办

    技术解除流量封顶,40G流量用完啦,怎么办联通腾讯王卡解除流量封顶,根据网络使用公平原则,在当月总流量达到40G后将会关闭上网功能,次月自动恢复。如当月要继续使用,可通过电子渠道开通“解除流量封顶服务”,开通后超出40G

    生活 2021年10月23日
  • 如何解决异步log4j2中的位置信息打印问题

    技术异步log4j2中的location信息打印问题怎么解决异步log4j2中的location信息打印问题怎么解决,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习

    攻略 2021年12月23日
  • JVM指令系统的组成及怎么用使用

    技术JVM指令系统的组成及怎么用使用JVM指令系统的组成及怎么用使用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。介绍一下JVM指令系统 的概念和用法,JV

    攻略 2021年10月23日