Spring AOP之基于注解的使用

news2024/9/29 18:13:36

1、技术说明

AOP是思想,AspectJ是AOP思想的实现。

在这里插入图片描述

  • 动态代理(InvocationHandler):JDK原生的实现方式,需要被代理的目标类必须实现接口。因为这个技术要求代理对象和目标对象实现同样的接口(兄弟两个拜把子模式)。

  • cglib:通过继承被代理的目标类(认干爹模式)实现代理,所以不需要目标类实现接口。

  • AspectJ:本质上是静态代理,将代理逻辑“织入”被代理的目标类编译得到的字节码文件,所以最终效果是动态的。weaver就是织入器。Spring只是借用AspectJ中的注解。

有关动态代理和静态代理的前置知识,可以参考这篇博客->静态代理和JDK动态代理以及CGLIB动态代理

2、准备工作

①添加依赖

在IOC所需依赖基础上再加入依赖即可:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>spring-aop</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- junit测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring-aspects会帮我们传递过来aspectjweaver -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.1</version>
        </dependency>

    </dependencies>
</project>

②准备被代理的目标资源

接口:

package com.atguigu.aop.annotation;

public interface Calculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}

实现类:

package com.atguigu.aop.annotation;

import org.springframework.stereotype.Component;

@Component
public class CalculatorImpl implements Calculator{
    @Override
    public int add(int i, int j) {
        int result = i + j;
        System.out.println("方法内部 result = " + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result = i - j;
        System.out.println("方法内部 result = " + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        int result = i * j;
        System.out.println("方法内部 result = " + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        int result = i / j;
        System.out.println("方法内部 result = " + result);
        return result;
    }
}

3、创建切面类并配置

// @Aspect表示这个类是一个切面类
@Aspect
// @Component注解保证这个切面类能够放入IOC容器
@Component
public class LogAspect {
    @Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint){
	String methodName = joinPoint.getSignature().getName();
	String args = Arrays.toString(joinPoint.getArgs());
	System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
	}
    @After("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("Logger-->后置通知,方法名:"+methodName);
	}
    @AfterReturning(value = "execution(*com.atguigu.aop.annotation.CalculatorImpl.*(..))", returning = "result")
	public void afterReturningMethod(JoinPoint joinPoint, Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("Logger-->返回通知,方法名:"+methodName+",结果:"+result);
	} 
    @AfterThrowing(value = "execution(*com.atguigu.aop.annotation.CalculatorImpl.*(..))", throwing = "ex")
	public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("Logger-->异常通知,方法名:"+methodName+",异常:"+ex);
	}
    @Around("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
	public Object aroundMethod(ProceedingJoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
		String args = Arrays.toString(joinPoint.getArgs());
		Object result = null;
        try {
            System.out.println("环绕通知-->目标对象方法执行之前");
            //目标对象(连接点)方法的执行
            result = joinPoint.proceed();
            System.out.println("环绕通知-->目标对象方法返回值之后");
		} catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知-->目标对象方法出现异常时");
		} finally {
			System.out.println("环绕通知-->目标对象方法执行完毕");
		}
		return result;
	}
}

在Spring的配置文件中配置:

<?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:context="http://www.springframework.org/schema/context"
       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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--
     基于注解的AOP的实现:
     1、将目标对象和切面都要交给IOC容器管理(注解+扫描)
     2、开启AspectJ的自动代理,为目标对象自动生成代理
     3、切面类必须通过注解@Aspect标识
 -->
    <context:component-scan base-package="com.atguigu.aop.annotation"></context:component-scan>

<!--    开启基于注解的AOP-->
    <aop:aspectj-autoproxy />
</beans>

Demo1:

package com.atguigu.aop.annotation;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect // 将当前类标记为切面类
@Component
public class LoggerAspect {

    @Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.add(int,int))")
    public void beforeMethod(){
        System.out.println("LoggerAspect,前置通知");
    }
}
package com.atguigu.test;

import com.atguigu.aop.annotation.Calculator;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
    @Test
    public void testAOPBefore(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
        // 无法在IOC容器中获取目标对象,只能在IOC容器中获取代理对象
        Calculator calculator = ioc.getBean(Calculator.class); // AOP后通过接口类型进行获取
        calculator.add(1,1);
    }
}

输出:

LoggerAspect,前置通知
方法内部 result = 2

Demo2:

package com.atguigu.aop.annotation;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect // 将当前类标记为切面类
@Component
public class LoggerAspect {

//    @Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.add(int,int))")
    @Before("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))") // 通配表达式
    public void beforeMethod(){
        System.out.println("LoggerAspect,前置通知");
    }
}

4、各种通知

  • 前置通知:使用@Before注解标识,在被代理的目标方法执行
  • 返回通知:使用@AfterReturning注解标识,在被代理的目标方法返回值之后执行,有异常就不会执行(寿终正寝
  • 异常通知:使用@AfterThrowing注解标识,在被代理的目标方法catch子句中执行(死于非命
  • 后置通知:使用@After注解标识,在被代理的目标方法finally子句中执行(盖棺定论
  • 环绕通知:使用@Around注解标识,使用try…catch…finally结构围绕整个被代理的目标方法,包括上面四种通知对应的所有位置

各种通知的执行顺序:

  • Spring版本5.3.x以前:
    • 前置通知
    • 目标操作
    • 后置通知
    • 返回通知或异常通知
  • Spring版本5.3.x以后(当前笔记的版本):
    • 前置通知
    • 目标操作
    • 返回通知或异常通知
    • 后置通知

5、切入点表达式语法

①作用

在这里插入图片描述

②语法细节

  • 用*号代替“权限修饰符”和“返回值”部分表示“权限修饰符”和“返回值”不限
  • 在包名的部分,一个“”号只能代表包的层次结构中的一层,表示这一层是任意的。
    • 例如:.Hello匹配com.Hello,不匹配com.atguigu.Hello
  • 在包名的部分,使用“…”表示包名任意、包的层次深度任意
  • 在类名的部分,类名部分整体用号代替,表示类名任意
  • 在类名的部分,可以使用号代替类名的一部分
    • *例如:*Service匹配所有名称以Service结尾的类或接口
  • 在方法名部分,可以使用号表示方法名任意
  • 在方法名部分,可以使用号代替方法名的一部分
    • 例如:*Operation匹配所有方法名以Operation结尾的方法
  • ​ 在方法参数列表部分,使用(…)表示参数列表任意
  • 在方法参数列表部分,使用(int,…)表示参数列表以一个int类型的参数开头
  • 在方法参数列表部分,基本数据类型和对应的包装类型是不一样的
    • 切入点表达式中使用 int 和实际方法中 Integer 是不匹配的
  • 在方法返回值部分,如果想要明确指定一个返回值类型,那么必须同时写明权限修饰符
    • 例如:execution(public int Service.(…, int)) 正确
    • 例如:execution( int *…Service.(…, int)) 错误

在这里插入图片描述

6、重用切入点表达式

针对某一个类中的切入点进行重用,方便后续的增强。

①声明

@Pointcut("execution(* com.atguigu.aop.annotation.*.*(..))")
public void pointCut(){}

②在同一个切面中使用

@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs());
    System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

③在不同切面中使用

@Before("com.atguigu.aop.CommonPointCut.pointCut()")
public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs());
    System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

7、获取通知的相关信息

①获取连接点信息

获取连接点信息可以在通知方法的参数位置设置JoinPoint类型的形参

@Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint){
    //获取连接点的签名信息
    String methodName = joinPoint.getSignature().getName();
    //获取目标方法到的实参信息
    String args = Arrays.toString(joinPoint.getArgs());
    System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

②获取目标方法的返回值

@AfterReturning中的属性returning,用来将通知方法的某个形参,接收目标方法的返回值

@AfterReturning(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))", returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint, Object result){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("Logger-->返回通知,方法名:"+methodName+",结果:"+result);
}

③获取目标方法的异常

@AfterThrowing中的属性throwing,用来将通知方法的某个形参,接收目标方法的异常

@AfterThrowing(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))", throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("Logger-->异常通知,方法名:"+methodName+",异常:"+ex);
}

8、环绕通知

相当于前面四个普通通知的合体

更像是动态代理,目标方法的执行,目标方法的返回值一定要返回给外界调用者。

@Around("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
// ProceedingJoinPoint joinPoint表示目标对象方法的执行,才能在执行的过程中加入额外的操作
public Object aroundMethod(ProceedingJoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs());
    Object result = null;
    try {
        System.out.println("环绕通知-->目标对象方法执行之前");
        //目标方法的执行,目标方法的返回值一定要返回给外界调用者
        result = joinPoint.proceed();
        System.out.println("环绕通知-->目标对象方法返回值之后");
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        System.out.println("环绕通知-->目标对象方法出现异常时");
    } finally {
        System.out.println("环绕通知-->目标对象方法执行完毕");
    }
    return result; // 一定要返回给调用者
}

9、切面的优先级

相同目标方法上同时存在多个切面时,切面的优先级控制切面的内外嵌套顺序。

  • 优先级高的切面:外面
  • 优先级低的切面:里面

使用@Order注解可以控制切面的优先级:

  • @Order(较小的数):优先级高
  • @Order(较大的数):优先级低

在这里插入图片描述

package com.atguigu.aop.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(1) // 设置切面优先级
public class ValidateAspect {
    @Around("com.atguigu.aop.annotation.LoggerAspect.pointCut()")
    public Object AroundAdviceMethod(ProceedingJoinPoint joinPoint){
        Object result=null;
        try {
            System.out.println("ValidateAspect环绕通知--》前置通知");
            result = joinPoint.proceed();
            System.out.println("ValidateAspect环绕通知--》返回通知");
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("ValidateAspect环绕通知--》异常通知");
        } finally {
            System.out.println("ValidateAspect环绕通知--》后置通知");
        }
        return result;
    }
}
package com.atguigu.aop.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect // 将当前类标记为切面类
@Component
public class LoggerAspect {


    @Pointcut("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
    public void pointCut(){}

    @Around("pointCut()")
    public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
        Object result=null;
        try {
            System.out.println("LoggerAspect环绕通知--》前置通知");
            result = joinPoint.proceed();
            System.out.println("LoggerAspect环绕通知--》返回通知");
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("LoggerAspect环绕通知--》异常通知");
        } finally {
            System.out.println("LoggerAspect环绕通知--》后置通知");
        }
        return result;
    }
}

测试:

package com.atguigu.test;

import com.atguigu.aop.annotation.Calculator;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
    @Test
    public void testAOPBefore(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
        Calculator calculator = ioc.getBean(Calculator.class); // AOP后通过接口类型进行获取
        calculator.add(1,1);
    }
}

输出:

ValidateAspect环绕通知--》前置通知
LoggerAspect环绕通知--》前置通知
方法内部 result = 2
LoggerAspect环绕通知--》返回通知
LoggerAspect环绕通知--》后置通知
ValidateAspect环绕通知--》返回通知
ValidateAspect环绕通知--》后置通知

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/370779.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【SPSS】单样本T检验分析详细操作教程(附案例实战)

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

服务端开发之Java备战秋招面试3

今天继续学习&#xff0c;先做两题算法题练练手&#xff0c;在继续整理八股文&#xff0c;深入理解&#xff0c;才能在面试的时候有更好地表现&#xff0c;一起加油吧&#xff0c;希望秋招多拿几个令人心动的offer&#xff0c;冲吧。 目录 1、算法题&#xff1a;判断链表中是…

带你了解IP报警柱的特点

IP可视报警柱是一款室外防水紧急求助可视对讲终端。安装在学校、广场、道路人流密集和案件高发区域&#xff0c;当发生紧急情况或需要咨询求助时按下呼叫按钮立即可与监控中心值班人员通话&#xff0c;值班人员也可通过前置摄像头了解现场情况并广播喊话。IP可视报警柱的使用特…

【双重注意机制:肺癌:超分】

Dual attention mechanism network for lung cancer images super-resolution &#xff08;肺癌图像超分辨率的双重注意机制网络&#xff09; 目前&#xff0c;肺癌的发病率和死亡率均居世界恶性肿瘤之首。提高肺部薄层CT的分辨率对于肺癌筛查的早期诊断尤为重要。针对超分辨…

收割不易,五面Alibaba终拿Java岗offer

前言 前段时间有幸被阿里的一位同学内推&#xff0c;参加了阿里巴巴Java岗位的面试&#xff0c;本人19年双非本科软件工程专业&#xff0c;目前有一年半的工作经验&#xff0c;面试前就职于一家外包公司。如果在自己本人拿到offer之前&#xff0c;如果有人告诉我一年工作经验可…

会声会影2023专业版视频处理制作软件功能详细介绍

会声会影是一款专业的视频处理和制作软件&#xff0c;也是目前影楼制作结婚和一般视频特效制作的必备软件&#xff0c;他是一款专为个人及家庭所设计的数码影片编辑软件&#xff0c;可将数 字或模拟摄像机所拍下来的如成长写真、国外旅游、个人MTV、生日派对、毕业典礼等精彩生…

C++ 修改程序进程的优先级(Linux,Windows)

文章目录1、Linux1.1 常用命令1.1.1 不占用终端运行和后台运行方式1.1.2 查询进程1.1.3 结束进程1.1.4 优先级命令1.2 C 代码示例1.2.1 代码一1.2.2 代码二2、Windows2.1 简介2.2 函数声明2.3 C 代码示例2.3.1 代码一2.3.2 代码二结语1、Linux 1.1 常用命令 1.1.1 不占用终端…

关于死锁的一些基本知识

目录 死锁是什么&#xff1f; 死锁的三种经典情况 1.一个线程&#xff0c;一把锁&#xff0c;连续加锁两次&#xff0c;如果锁是不可重入锁就会死锁。 不可重入锁与可重入锁&#xff1a; 2.两个线程两把锁&#xff0c;t1和t2各自针对于锁A和锁B加锁&#xff0c;再尝试获取…

Redis 集群

文章目录一、集群简介二、Redis集群结构设计&#x1f349;2.1 数据存储设计&#x1f349;2.2 内部通信设计三、cluster 集群结构搭建&#x1f353;3-1 cluster配置 .conf&#x1f353;3-2 cluster 节点操作命令&#x1f353;3-3 redis-trib 命令&#x1f353;3-4 搭建 3主3从结…

用ChatGPT生成Excel公式,太方便了

ChatGPT 自去年 11 月 30 日 OpenAI 重磅推出以来&#xff0c;这款 AI 聊天机器人迅速成为 AI 界的「当红炸子鸡」。一经发布&#xff0c;不少网友更是痴迷到通宵熬夜和它对话聊天&#xff0c;就为了探究 ChatGPT 的应用天花板在哪里&#xff0c;经过试探不少人发现&#xff0c…

同步和非同步整流DC/DC转换区别

在DC/DC转换器中&#xff0c;非隔离式降压开关稳压器包括两种拓扑结构&#xff1a;非同步整流&#xff08;二极管&#xff09;型和同步整流型。非同步整流型已经使用多年&#xff0c;具有简单的开关稳压器电路&#xff0c;效率勉强超过80%。随后&#xff0c;电池供电应用&#…

VMware 的网络适配器 桥接-NAT-仅主机

大家使用VMware安装镜像之后&#xff0c;是不是都会考虑虚拟机的镜像系统怎么连上网的&#xff0c;它的连接方式是什么&#xff0c;它ip是什么&#xff1f; 路由器、交换机和网卡 1.路由器 一般有几个功能&#xff0c;第一个是网关、第二个是扩展有线网络端口、第三个是WiFi功…

Redis 被问麻了...

Redis是面试中绕不过的槛&#xff0c;只要在简历中写了用过Redis&#xff0c;肯定逃不过。今天我们就来模拟一下面试官在Redis这个话题上是如何一步一步深入&#xff0c;全面考察候选人对于Redis的掌握情况。 小张&#xff1a; 面试官&#xff0c;你好。我是来参加面试的。 …

Hadoop-MapReduce

Hadoop-MapReduce 文章目录Hadoop-MapReduce1 MapRedcue的介绍1.1 MapReduce定义1.2 MapReduce的思想1.3MapReduce优点1.4MapReduce的缺点1.5 MapReduce进程1.6 MapReduce-WordCount1.6.1 job的讲解2 Hadoop序列化2.1 序列化的定义2.2 hadoop序列化和java序列化的区别3 MapRedu…

RabbitMQ发布确认模式

目录 一、发布确认原理 二、发布确认的策略 &#xff08;一&#xff09;开启发布确认的方法 &#xff08;二&#xff09;单个确认模式 &#xff08;三&#xff09;批量确认模式 &#xff08;四&#xff09;异步确认模式 &#xff08;五&#xff09;如何处理异步未确认消…

华为CT6100双千M路由记录

该文章仅仅记录使用CT6100的流程&#xff0c;不提供任何参考和建议。 一、简介 设备&#xff1a;华为CT6100瘦客服端&#xff0c;J1800cpu&#xff0c;不包含外壳&#xff0c;有双千M网口&#xff0c;2G内存8G硬盘。系统&#xff1a;esir的高大全openwrt版本用途&#xff1a;对…

QT 完美实现圆形按钮

QT 版本&#xff1a;5.6.0 官方的按钮有些普通&#xff0c;如果我们想要换成自己喜欢的按钮而却无从下手&#xff0c;那么请继续往下阅读&#xff08;皮一下&#xff09;。 首先&#xff0c;可以在网络上搜索一下自己喜欢的按钮图形&#xff08;或者可以自行绘制&#xff09;…

十大算法基础——上(共有20道例题,大多数为简单题)

一、枚举&#xff08;Enumerate&#xff09;算法 定义&#xff1a;就是一个个举例出来&#xff0c;然后看看符不符合条件。 举例&#xff1a;一个数组中的数互不相同&#xff0c;求其中和为0的数对的个数。 for (int i 0; i < n; i)for (int j 0; j < i; j)if (a[i] …

偏向锁、轻量级锁、自旋锁、重量级锁,它们都是什么?有什么关联

互斥锁的本质是共享资源。 当有多个线程同时对一个资源进行操作时&#xff0c;为了线程安全&#xff0c;要对资源加锁。 更多基础内容参看上文《深入了解Java线程锁(一)》 接下来&#xff0c;我们来看看两个线程抢占重量级锁的情形&#xff1a; 上图讲述了两个线程ThreadA和…

SMART PLC斜坡函数功能块(梯形图代码)

斜坡函数Ramp的具体应用可以参看下面的文章链接: PID优化系列之给定值斜坡函数(PLC代码+Simulink仿真测试)_RXXW_Dor的博客-CSDN博客很多变频器里的工艺PID,都有"PID给定值变化时间"这个参数,这里的给定值变化时间我们可以利用斜坡函数实现,当然也可以利用PT1…