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)

相关推荐

  • 分享CSS3边框

    技术分享CSS3边框本篇内容主要讲解“分享CSS3边框”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“分享CSS3边框”吧!浏览器支持实例使用图片创建围绕 div 元素的边框:

    攻略 2021年11月6日
  • 推荐一下我的angular ui模版之我的daming-core-ui更新到angular12了

    技术推荐一下我的angular ui模版之我的daming-core-ui更新到angular12了 推荐一下我的angular ui模版之我的daming-core-ui更新到angular12了前言

    礼包 2021年11月9日
  • 大数据处理涉及的数据类型(数据库实时查询大数据)

    技术数据库中如何操作大数据集这篇文章给大家分享的是有关数据库中如何操作大数据集的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、子查询插入数据1、语法INSERT INTO table [ c

    攻略 2021年12月13日
  • 何鸿燊怎么读音,澳门赌王何鸿燊的拼音怎么拼

    技术何鸿燊怎么读音,澳门赌王何鸿燊的拼音怎么拼何鸿燊读音:hé hóng shēn 何鸿燊(shēn),1921年11月25日生于香港,著名港澳企业家,有“澳门赌王”之称,是何东爵士弟何福的孙儿之一,何世光儿子,在家中排

    生活 2021年10月23日
  • 微服务容器化用docker还是k8(docker适合于微服务的特点)

    技术基于微服务和Docker容器技术是什么这篇文章主要讲解了“基于微服务和Docker容器技术是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于微服务和Docker

    攻略 2021年12月13日
  • 应针对seo搜索引擎工作原理应该进行的优化有哪些

    技术应针对seo搜索引擎工作原理应该进行的优化有哪些今天就跟大家聊聊有关应针对seo搜索引擎工作原理应该进行的优化有哪些,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收

    攻略 2021年12月3日