# SpringBoot实验二 **Repository Path**: kevinsoft/spring_boot_experiment_2 ## Basic Information - **Project Name**: SpringBoot实验二 - **Description**: 大三下学期,企业级开发框架专题 实验二 利用Spring boot的自动装配特性实现动态注册组件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2020-04-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 实验二 利用Spring boot的自动装配特性实现动态注册组件 ## 一、自动装配 1. 创建一个自定义的CommandLineRunner接口的实现类。 ![image-20200408014428899](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408014428899.png) 2. 创建一个自定义的自动配置类,给自动配置类添加有效条件 ![image-20200408014516037](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408014516037.png) 3. 自定义的一个Bean,绑定属性值,并生成spring配置类的元数据文件 ![image-20200408014751867](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408014751867.png) 4. 创建spring.factories文件 ![image-20200408014558186](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408014558186.png) 5. 在application.properties属性文件中添加一个自定义的属性 ![image-20200408014850779](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408014850779.png) 6. 运行 ![image-20200408015034891](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015034891.png) ## 二、自定义事件发布器 1. 自定义事件发布器,配置线程池使其支持多线程异步处理事件 ![image-20200408015156155](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015156155.png) 2. 自定义事件监听器 ![image-20200408015246050](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015246050.png) 3. 自定义事件实体类 ![image-20200408015310037](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015310037.png) 4. 编写单元测试 ![image-20200408015420153](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015420153.png) 5. 运行 ![image-20200408015444446](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015444446.png) ## 三、自定义线程池 1. 查看TaskExecutionAutoConfiguration源码,发现如果没有Executor类型的bean就会自动为我们创建默认的线程池 ![image-20200408015655691](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015655691.png) 2. 自定义线程池 ![image-20200408015951196](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408015951196.png) 3. 断点调试验证是否生效 ![image-20200408020136650](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408020136650.png) ![image-20200408020202600](https://gitee.com/kevinsoft/spring_boot_experiment_2/raw/master/images/image-20200408020202600.png) taskExecutor对象引用为同一个,自定义线程池生效。