Spring深入学习

news2024/7/6 17:44:25

1 Bean创建的生命周期

Spring bean是Spring运行时管理的对象。Spring Bean的生命周期指的是Bean从创建到初始化再到销毁的过程,这个过程由IOC容器管理。

IOC即控制反转,是面向对象编程中的一种设计原则,通过依赖注入(DI)、依赖查找的方式实现对象之间的松耦合关系。

BeanFactory为IoC容器,ApplicationContext为应用上下文,ApplicationContext容器包括BeanFactory容器的所有功能。

1)创建bean的三种方式

  1. 基于XML配置文件
  2. 基于注解,@Component、 @Repository、@Service、@Controller;@Component可以代替@Repository、@Service、@Controller。
  3. 基于Java类的bean定义,需要提供setter方法
    @Bean
    public Student student(){
        return new Student();;
    }


    public class Student{
        private String name;
  
        public void SetName(String name){
            this.name = name;
        }
    }

 2)bean对象

 

对象不一定是bean,bean一定是对象,bean对象都放在一个MAP里。 

获取对象的方式可以使用构造方法去创建对象,上面的UserService就存在一个默认的构造方法。(如果程序中没有显式定义任何构造方法,那么java语言将自动提供一个隐含的默认构造方法。

spring扫描到@Component等注解时,就会认为这是定义的bean,就会使用此类构造方法获取对象。然后Spring去检查哪些对象存在@Autowired,就给自动进行依赖注入,进行赋值。

UserServie.class--->构造方法--->普通对象--->依赖注入--->放入Map<beanName,Bean对象>

spring会继续去检查哪些方法存在@PostConstruct方法,然后主动执行方法里的内容。 

实现逻辑如下:

当然也可以实现InitializingBean接口,用写afterPropertiesSet()方式实现

((InitializingBean)对象).afterPropertiesSet(); 

UserServie.class--->推断构造方法--->普通对象--->依赖注入--->初始化前(@PostConstruct)--->初始化(InitializingBean)--->初始化后(AOP)--->代理对象--->放入Map<beanName,Bean对象>

推断构造方法:

@Service
public class UserService {
    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

写了无参构造,则默认使用无参构造依赖注入;

不写无参构造,写多个有参构造,会报错No Default Construct;必须使用@Autowired指定默认是是哪个;

不写无参构造,只写一个有参构造,则会直接使用这个有参构造进行依赖注入;

依赖注入完成属性赋值,spring会依据入参的类型、名字去spring IOC容器里的Bean MAP<beanname,bean对象>里寻找bean对象。

1.1 依赖注入

在Java中,DI的实现方式主要有以下几种:

● 构造器注入
● Setter方法注入
● 接口注入
● 注解注入

1)构造器注入(spring框架中在构造方法上添加@Autowired注解)

@Component 标准一个普通的spring Bean类; @Repository标注一个DAO组件类; @Service标注一个业务逻辑组件类。 @Component可以代替@Repository、@Service、@Controller,因为这三个注解是被@Component标注的。

@Service
public class UserService {
    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User getUserById(int id) {
        return userRepository.getUserById(id);
    }
}

2)setter方法注入(在setter方法上添加@Autowired注解)

@Service
public class UserService {
    private UserRepository userRepository;

    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User getUserById(int id) {
        return userRepository.getUserById(id);
    }
}

3)接口注入

@Service
public class UserService {
    private UserRepository userRepository;

    @Autowired
    public void setUserRepositorySetter(UserRepositorySetter userRepositorySetter) {
        userRepositorySetter.setUserRepository(userRepository);
    }

    public User getUserById(int id) {
        return userRepository.getUserById(id);
    }
}

实现对应的接口

public interface UserRepositorySetter {
    void setUserRepository(UserRepository userRepository);
}

@Repository
public class UserRepositoryImpl implements UserRepository, UserRepositorySetter {
    @Override
    public User getUserById(int id) {
        // 实现代码
    }

    @Override
    public void setUserRepository(UserRepository userRepository) {
        // 实现代码
    }
}

4)注解注入

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public User getUserById(int id) {
        return userRepository.getUserById(id);
    }
}

依赖注入如何寻找是哪个 userRepository?过程如下,从by type 到 by name:

1.2 AOP

认识AOP

AOP 是 Aspect Oriented Programming 的缩写,译为面向切向编程。

设计一个日志打印模块: 

  • 按 OOP 思想,我们会设计一个打印日志 LogUtils 类,然后在需要打印的地方引用即可。
  • 按AOP思想,声明哪些地方需要打印日志,这个地方就是一个切面,AOP 会在适当的时机为你把打印语句插进切面。

AOP实现的关键在于AOP框架自动创建的AOP代理,以AspectJ为代表的静态代理,以Spring AOP为代表的动态代理。Spring AOP中的动态代理主要有两种方式:JDK动态代理和CGLIB动态代理。

Spring AOP通过在代理类中包裹切面,Spring在运行期把切面织入到Spring管理的bean中。代理类封装了目标类,并拦截被通知方法的调用,再把调用转发给真正的目标bean(目标对象)。

AOP实现技术由APT、AspetJ等,如下:

1)横切关注点

跨越应用程序多个模块的方法或功能,如日志、安全、缓存、事务等等。

2)连接点

连接点是在应用执行中能够插入切面的一个点。即程序执行过程中能够应用通知的所有点。

3)通知

切面的工作被称为通知,Spring切面可以应用5种类型的通知:

  • 前置通知(Before):在目标方法被调用之前调用通知功能。
  • 后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么。
  • 返回通知(After-returning):在目标方法成功执行之后调用通知。
  • 异常通知(After-throwing)):在目标方法抛出异常后调用通知。
  • 环绕通知(Around) :通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。

1.3 SpringBoot AOP

orderService为空? 

进入test之前,orderServcie是有值的!

1)Spring首先要判断这个对象要不要执行AOP

2)生成一个子类代理类,继承UserService,重写UserService里的test方法

3)子类代理类(代理对象)执行test方法时,先执行切面逻辑,然后执行业务逻辑

4)执行 代理对象.target.test()方法

        代理对象.target对象 = 被代理对象 = 普通对象 = 已经经过了依赖注入 = 对象已经属性有值;

        但是代理对象没有依赖注入,没有值;他只是为了执行切面逻辑,所以不需要必须去用对象的属性。

切面逻辑中可以通过获取这个target对象,来获取普通对象

AOP基本使用: 

依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

代码
@Aspect  // 使用@Aspect注解声明一个切面
@Component
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;

    @Pointcut("@annotation(com.lmlsj.SysLog)")
    public void logPointCut() {}


    @Before("execution(* com.lmlsj.SysLog.*(..))")
    public void before() {
        System.out.println("MyAspect: 前置增强.....");
    }
}

配置
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {

}

正常流程:【环绕通知-前】-> 【前置通知】-> 【返回通知】-> 【后置通知】->【环绕通知-后】。 

2 事物

1)事物执行逻辑

2)普通对象调用,事物失效

这里a()方法是普通对象的,直接执行a()里面的语句,不会去走切面逻辑判断是否有@Transactional注解,事物失效。 

3)代理对象调用才会去走切面逻辑

自己注入自己,使用代理对象实现事物正常执行

4)@Configuration

加上@Configuration后,Appconfig就是代理对象,才能保证下图两处的dataSource()是同一个对象,才能执行事务。

3 扫描

Spring扫描首先Spring根据注解去寻找bean类,非懒加载的bean在Spring容器启动时就创建好,懒加载的bean是用到时再创建。如何寻找注解的实现思路:

1)反射方式:AA.class.isAnnotationPresent(Component.class)

2)ASM技术(Spring使用):编辑CLASS字节码

扫描类的寻找:

AnnotationConfigApplicationContext.java

1)处理配置类

(1)判断Component注解

ConfigurationClassParser.java  doProcessConfigurationClass(*)

(2)检查是不是配置类的地方

2)ComponentScan

parse解析得到BeanDefinition集合,然后去遍历BeanDefinition这些对象是不是有什么注解、配置

 3)parse属性解析过程

ComponentScanAnnotationParser类

this.registry是Spring IOC容器

(1) useDefaultFilters属性,注册一些默认的过滤器

(2)ComponentScan的nameGenerator属性

generatorClass是nameGenerator属性的管理,没有设置该属性就使用默认值。

补充:可以根据@Component的value设置bean的名字,没有设置就根据类名进行设置。

前两个字符都是大写,就直接返回;不符合就将第一个字符,设置为小写。

(3)scopeProxy属性设置

作用域在类上设置,在收到请求时再创建bean

同时可以指定代理对象的生成方式

(4)resourcePattern、includeFilters、excludeFilters属性

excludeFilters可以设置某个类不是一个bean,type = FilterType.ASSIGNABLE_TYPE是根据类class来过滤

@ComponentScan(value = "com.lmlsj", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {MyService.class})})

(5)懒加载

(6)扫描路径属性

根据属性值配置扫描路径,或者根据@ComponentScan注解配置扫描路径

排除器,排除已经设置成bean的类 

最后就是doScan真正的扫描开始。

4)doScan扫描

扫描包路径

 public Set<BeanDefinition> findCandidateComponents(String basePackage) {
        return this.componentsIndex != null && this.indexSupportsIncludeFilters() 
        ? 
        this.addCandidateComponentsFromIndex(this.componentsIndex, basePackage) : 
        this.scanCandidateComponents(basePackage);
    }

检查是否在过滤器里

4 Spring启动demo

1)SpringApplicationContex

public class SpringApplicationContext {

    private Class configClass;
    private Map<String,BeanDefination> beanDefinationMap = new HashMap<>();
    private Map<String,Object> singletonObjects = new HashMap<>();  //单例池
    private List<BeanPostProcess> beanPostProcessorList = new ArrayList<>();

    public SpringApplicationContext(Class configClass) {
        this.configClass = configClass;
        scan(configClass);              //解析传进来的配置类,生成beanDefination
        preInstantiateSingletons();     //单例bean初始化
    }

    private void preInstantiateSingletons() {
        for(Map.Entry<String,BeanDefination> entry : beanDefinationMap.entrySet()){
            BeanDefination beanDefination = entry.getValue();
            if(beanDefination.getScope().equals("singleton")){
                Object bean = createBean(entry.getKey(), beanDefination);
                singletonObjects.put(entry.getKey(), bean);     //单例bean
            }
        }
    }

    public Object createBean(String beanName, BeanDefination beanDefination){
        Class clazz = beanDefination.getClazz();
        Object instance = null;
        try {
            // 1 对象实例化
            instance = clazz.newInstance();

            // 2 依赖注入 属性赋值
            for(Field field : clazz.getDeclaredFields()){
                if(field.isAnnotationPresent(LmAutowired.class)){
                    String name = field.getName();  // field.getType()  ,by name/ by type
                    Object bean = getBean(name);
                    field.setAccessible(true);
                    field.set(instance,bean);
                }
            }

            // 4 初始化前
            for (BeanPostProcess beanPostProcess : beanPostProcessorList) {
               instance =  beanPostProcess.postProcessBeforeInitialization(instance,beanName);
            }

            // 3 spring初始化数值
            if(instance instanceof InitializingBean){
                try {
                    ((InitializingBean)instance).afterPropertiesSet();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            // 5 初始化后
            for (BeanPostProcess beanPostProcess : beanPostProcessorList) {
               instance =  beanPostProcess.postProcessAfterInitialization(instance,beanName);
            }

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return instance;
    }

    public void scan(Class configClass){
        // 1 解析配置类
        if(configClass.isAnnotationPresent(LmComponentScan.class)){
            LmComponentScan componentScan = (LmComponentScan)configClass.getAnnotation(LmComponentScan.class);
            String path = componentScan.value();
            System.out.println("配置路径: " + path);
            path = path.replace(".","/");

            //2 扫描配置路径下有LmComponent注解的类
            ClassLoader classLoader = SpringApplicationContext.class.getClassLoader();  //app
            URL resource = classLoader.getResource(path);

            File file = new File(resource.getFile()); //获取对应文件夹
            File[] files = file.listFiles();//文件夹下所有的文件

            for (File f : files) {
                String fileName = f.getAbsolutePath();
                if(fileName.endsWith(".class")){
                    String className = fileName.substring(fileName.indexOf("com"),fileName.indexOf(".class"));
                    className = className.replace("/",".");
                    try {
                        Class clazz = classLoader.loadClass(className);
                        //spring是使用ASM去解析字节码文件的,这里简单实现一下
                        if(clazz.isAnnotationPresent(LmComponent.class)){

                            //beanPostProcessorList初始化
                            if(BeanPostProcess.class.isAssignableFrom(clazz)){
                                try {
                                    BeanPostProcess beanPostProcess = (BeanPostProcess) clazz.newInstance();
                                    beanPostProcessorList.add(beanPostProcess);
                                } catch (InstantiationException e) {
                                    e.printStackTrace();
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                            }

                            LmComponent annotation = (LmComponent) clazz.getAnnotation(LmComponent.class);
                            String beanName = annotation.value();
                            //3 生成beanDefination对象
                            BeanDefination beanDefination = new BeanDefination();

                            beanDefination.setClazz(clazz);

                            if(clazz.isAnnotationPresent(Scope.class)){
                                Scope scope = (Scope) clazz.getAnnotation(Scope.class);
                                beanDefination.setScope(scope.value());
                            }else{
                                beanDefination.setScope("singleton");
                            }

                            beanDefinationMap.put(beanName,beanDefination);
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            System.out.println("没有LmComponentScan注解");
        }
    }

    public Object getBean(String beanName){
        //1 判断是否已存在bean
        if(beanDefinationMap.containsKey(beanName)){
            BeanDefination beanDefination = beanDefinationMap.get(beanName);
            //2 判断是不是单例bean
            if(beanDefination.getScope().equals("singleton")){
                Object o = singletonObjects.get(beanName);
                return o;
            }else{
                //多例bean
                Object bean = createBean(beanName,beanDefination);
                return bean;
            }
        }else {
            System.out.println("没有找到" + beanName);
            throw new NullPointerException();
        }
    }

}

2)BeanDefination

public class BeanDefination {

    private Class clazz;
    private String scope;

    public Class getClazz() {
        return clazz;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }
    
}

3)BeanPostProcess

public interface BeanPostProcess {

    Object postProcessBeforeInitialization(Object bean,String beanName);

    Object postProcessAfterInitialization(Object bean,String beanName);
}

4)InitializingBean

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

5)注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface LmAutowired {

    String value() default ""; //属性
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LmComponent {

    String value() default ""; //属性
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LmComponentScan {

    String value() default ""; //属性
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Scope {
    String value() default ""; //属性
}

6)测试类

public class Main {

    public static void main(String[] args){
        //用spring 包里自己写的代码
        SpringApplicationContext springApplicationContext = new SpringApplicationContext(AppConfig.class);

        //多例bean
        UserService userService =  (UserService)springApplicationContext.getBean("userService");
        UserService userService2 =  (UserService)springApplicationContext.getBean("userService");
        UserService userService3 =  (UserService)springApplicationContext.getBean("userService");
        System.out.println(userService);
        System.out.println(userService2);
        System.out.println(userService3);

        userService.test();

        System.out.println("_____________________");
        //单例bean
        OrderService orderService = (OrderService)springApplicationContext.getBean("orderService");
        OrderService orderService2 = (OrderService)springApplicationContext.getBean("orderService");
        OrderService orderService3 = (OrderService)springApplicationContext.getBean("orderService");
        System.out.println(orderService);
        System.out.println(orderService2);
        System.out.println(orderService3);
    }
}

7)BeanPostProcesorImpl

@LmComponent
public class BeanPostProcesorImpl implements BeanPostProcess {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if(beanName.equals("orderService")){
            System.out.println("初始化之前");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        //代理AOP在此实现
        return bean;
    }
}

8)AppConfig

@LmComponentScan("com.lmlsj.test")
public class AppConfig {
}

9)其他类

@LmComponent("orderService")
public class OrderService {
}

@LmComponent("userDao")
public class UserDao {
}

@LmComponent("userService")
@Scope("prototype")
public class UserService implements InitializingBean {

    @LmAutowired
    private UserDao userDao;

    private User defaultUser;

    public void test(){
        System.out.println(userDao);
        System.out.println(defaultUser.getName() + ":" + defaultUser.getPass());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        defaultUser = new User("default","123456");
    }
}


10)测试结果

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

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

相关文章

TrustZone之中断及中断处理

一、中断 接下来,我们将查看系统中的中断,如下图所示: 通用中断控制器(GIC)支持TrustZone。每个中断源,在GIC规范中称为INTID,分配到以下三个组之一: • Group0:安全中断,以FIQ方式发出信号 • 安全Group1:安全中断,以IRQ或FIQ方式发出信号 • 非安全Gr…

SuperMap iManager 11i(2023) SP1新特性汇总

作者&#xff1a;ls 【目录】 &#xff08;一&#xff09;GIS云套件支持发布聚合服务&#xff08;二&#xff09;GIS云套件支持自定义/修改服务接口信息&#xff08;三&#xff09;GIS云套件管理界面支持批量修改镜像&#xff08;四&#xff09;GIS云套件管理界面可调整服务节点…

iA Writer for Mac:释放创作力的专业MarkDown写作软件

iA Writer for Mac是一款专业的MarkDown写作软件&#xff0c;为Mac用户提供了简洁、高效的写作环境。无论您是一名专业写作人员、学生或博主&#xff0c;iA Writer都能帮助您以最简单的方式将想法转化为文字。 首先&#xff0c;iA Writer的界面非常简洁&#xff0c;只展示您正…

MATLAB 系统辨识 + PID 自动调参

MATLAB 系统辨识 PID 自动调参 Matlab R2021b下载安装详细教程Chapter1 MATLAB 系统辨识 PID 自动调参1. 导入数据2. 系统辨识3. PID 自动调参 Chapter2 MATLAB系统辨识Chapter3 【MATLAB】使用系统辨识工具箱(System Identification)建模Chapter4 matlab系统辨识工具箱及其反…

网络服务IP属地发生变化的原因有哪些?

近期&#xff0c;许多用户发现自己的网络服务IP属地发生了变化。原本固定的IP地址不再是静态的&#xff0c;而是发生了变动。这一现象引起了广大用户的关注和疑惑&#xff0c;对网络服务的使用和信息安全产生了影响。为了解决用户的疑虑&#xff0c;我们对此现象进行了深入探究…

云仓酒庄带您品法国葡萄酒

说起葡萄酒肯定绕不开法国&#xff0c;法国葡萄酒闻名中外&#xff0c;口碑卓越。作为世界上的产酒大国&#xff0c;可以说是每一寸土地都可以种植葡萄。云仓酒庄的品牌雷盛红酒分享这么优秀的一个葡萄酒产酒国有哪些特点呢&#xff1f; 1.产区特色&#xff1a;波国有最著名的…

js输入框部分内容不可编辑,其余正常输入,el-input和el-select输入框和多个下拉框联动后的内容不可修改

<tr>//格式// required自定义指令<e-td :required"!read" label><span>地区&#xff1a;</span></e-td><td>//v-if"!read && this.data.nationCode 148"显示逻辑<divclass"table-cell-flex"sty…

百华鞋业入围全国老年用品标准编制参编单位

12月8日&#xff0c;中国轻工业信息中心组织召开了中国轻工业适老系列标准研讨会。中国轻工业联合会信息统计部&#xff08;中国轻工业信息中心&#xff09;副主任马真出席会议并讲话。中国轻工业信息中心标准工作负责人孟慧敏介绍了相关标准制定情况。会议由中国轻工业信息中心…

Cellinx NVT 摄像机 GetFileContent.cgi任意文件读取漏洞 (CVE-2023-23063)

0x01 产品简介 Cellinx NVT IP PTZ是韩国Cellinx公司的一个摄像机设备。 0x02 漏洞概述 Cellinx NVT v1.0.6.002b版本存在安全漏洞&#xff0c;该漏洞源于存在本地文件泄露漏洞&#xff0c;攻击者可读取系统密码等敏感信息。 0x03 复现环境 FOFA&#xff1a;body"loc…

LAMP平台——构建PHP运行环境

在构建LAMP平台时&#xff0c;各组件的安装顺序依次为Linux、Apache、MySQL、PHP。其中Apache和 MySQL的安装并没有严格的顺序&#xff1b;而PHP环境的安装一般放到最后&#xff0c;负责沟通Web服务器和数据库 系统以协同工作。 PHP 即 Hypertext Preprocessor&#xff08;超级…

连续型随机变量的概率密度

如果对于随机变量的分布函数&#xff0c;存在非负可积函数&#xff0c;使得对于任意实数&#xff0c;有&#xff1a; 那么就称为连续型随机变量&#xff0c;称为的概率密度函数&#xff0c;简称密度函数。

Spring 6(一)【Spring 入门】

前言 好久没有写博客了&#xff0c;最近刚忙完考试&#xff0c;眼下又是英语四六级。Flink 按说应该是大数据学习的主线任务&#xff0c;但是长时间学一门技术还是心累的。正好之前对 Java 注解有了进一步的熟悉&#xff0c;一直想着熟悉巩固。所以&#xff0c;今天开始就来深入…

Chapter 7 - 3. Congestion Management in Ethernet Storage Networks以太网存储网络的拥塞管理

Pause Threshold for Long Distance Links长途链路的暂停阈值 This section uses the following basic concepts: 本节使用以下基本概念: Bit Time (BT): It is the time taken to transmit one bit. It is the reciprocal of the bit rate. For example, BT of a 10 GbE po…

微信小程序ios中非cover组件点击重复触发地图tap事件

现象&#xff1a; map中使用view组件的click事件会重复触发地图的tap组件&#xff0c;只在ios上出现 <map id"maps" style"width: 100vw;height: 100vh;" :latitude"latitude" :longitude"longitude":markers"markers"…

P with Spacy:自定义文本分类管道

一、说明 Spacy 是一个功能强大的 NLP 库&#xff0c;其中许多 NLP 任务&#xff08;如标记化、词干提取、词性标记和命名实体解析&#xff09;均通过预训练模型提供开箱即用的功能。所有这些任务都由管道对象以及逐步应用于给定文本的不同函数的内部抽象来包装。该管道可以通过…

STM32_HAL库—IWDG看门狗

一、CubeMX设置 1、晶振配置&#xff08;72M&#xff09; 2、数据配置 超时时间 Tout prv / LSI * rlv (s) 其中prv是预分频器寄存器的值&#xff0c;rlv是重装载寄存器的值&#xff0c;而LSI值默认是40kHz&#xff0c;如下所示。 3、代码实现 int main(){while(1){HAL_IW…

C++ 学习系列 -- 模板 template

一 C 模板介绍&#xff1f; C 为什么引入模板&#xff1f; 我的理解是&#xff1a; C 引入模板的概念&#xff0c;是为了复用重复的代码&#xff0c;当某些代码除了操作的数据类型不同以外&#xff0c;其他逻辑全都相同&#xff0c;此时就适合采用模板的方式。 定义模板类或者…

Linux的文件系统 内核结构

Linux的文件系统 Q1&#xff1a;什么是文件系统&#xff1f; A&#xff1a;在学术的角度下&#xff0c;文件系统指“操作系统用于明确存储设备组织文件的方法”&#xff0c;是“文件管理系统”的简称&#xff0c;本质也是代码&#xff0c;一段程序 Q2&#xff1a;文件系统&…

使用opencv的Canny算子实现图像边缘检测

1 边缘检测介绍 图像边缘检测技术是图像处理和计算机视觉等领域最基本的问题&#xff0c;也是经典的技术难题之一。如何快速、精确地提取图像边缘信息&#xff0c;一直是国内外的研究热点&#xff0c;同时边缘的检测也是图像处理中的一个难题。早期的经典算法包括边缘算子方法…

代码随想录-刷题第二十八天

93. 复原 IP 地址 题目链接&#xff1a;93. 复原 IP 地址 思路&#xff1a;切割问题&#xff0c;原理上还是抽象成树形结构&#xff0c;然后使用回溯法。 131 题的要求是&#xff1a;让你把字符串 s 切分成若干个合法的回文串&#xff0c;返回所有的切分方法。 本题的要求是…