• SpringBoot很熟?那手撕一下自定义启动器吧


    一. 前言

    哈喽,大家好,不知道你有没有想辉哥呢?我可是很想你们哟!最近金九银十,又有不少小伙伴私信辉哥,说自己在面试时被问到SpringBoot如何自定义启动器,结果自己不知道该怎么回答。那么今天辉哥就手把手地带着大家,去看看在SpringBoot中到底该怎么实现自定义启动器。

    二. 什么是SpringBoot自动装配?

    在进行代码实现之前,我们先来看看什么是SpringBoot的自动装配。与自动装配对应的是手动装配,比如我们以前使用xml配置文件,引入spring或者引入mybatis时需要配置数据源、配置mybatis扫描、配置数据库连接池等。

    而在SpringBoot中,我们只需要引入对应的mybatis启动器、druid启动器和数据库驱动,配置文件就可以自动根据配置数据库地址、用户和密码等信息快速地完成框架的搭建。也就是说,我们只要引入启动器,再配置一些必要的初始化连接参数,就可以直接使用,而不需要再自己创建很多复杂的配置,就可完成Bean类之间的依赖。

    这一切都依赖于SpringBoot的自动装配!而自动装配则离不开starter启动器这个核心!那么starter启动器是怎么实现的呢?为了让大家搞明白这个问题,辉哥就给大家手写一个自定义的启动器。

    三. 自定义启动器

    1.第一步:首先创建一个java-maven的父工程

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.     <modelVersion>4.0.0</modelVersion>
    6.     <groupId>com.qfedu</groupId>
    7.     <artifactId>springboot-auto</artifactId>
    8.     <version>1.0-SNAPSHOT</version>
    9.     <packaging>pom</packaging>
    10.     
    11.     <parent>
    12.         <groupId>org.springframework.boot</groupId>
    13.         <artifactId>spring-boot-starter-parent</artifactId>
    14.         <version>2.1.3.RELEASE</version>
    15.         <relativePath/>
    16.     </parent>
    17. </project>

    2.第二步:创建一个子工程

    3.引入依赖

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.     <parent>
    6.         <artifactId>springboot-auto</artifactId>
    7.         <groupId>com.qfedu</groupId>
    8.         <version>1.0-SNAPSHOT</version>
    9.     </parent>
    10.     <modelVersion>4.0.0</modelVersion>
    11.     <artifactId>demo</artifactId>
    12.     
    13.     <dependencies>
    14.         <dependency>
    15.             <groupId>org.springframework.boot</groupId>
    16.             <artifactId>spring-boot-starter-web</artifactId>
    17.         </dependency>
    18.         <dependency>
    19.             <groupId>org.springframework.boot</groupId>
    20.             <artifactId>spring-boot-starter-test</artifactId>
    21.             <scope>test</scope>
    22.         </dependency>
    23.     </dependencies>
    24. </project>

    4.第三步:创建另一个子模块 java2113-starter作为启动器

    5.引入依赖

    1. <dependencies>
    2.     
    3.     <dependency>
    4.         <groupId>org.springframework.boot</groupId>
    5.         <artifactId>spring-boot-autoconfigure</artifactId>
    6.     </dependency>
    7. </dependencies>

    6.第四步:编写代码

    1. package com.qfedu.java2113;
    2. public class HelloService {
    3.     private String msg;
    4.     public String sayHello(){
    5.         return "hello" + msg;
    6.     }
    7.     
    8.     public String getMsg() {
    9.         return msg;
    10.     }
    11.     
    12.     
    13.     public void setMsg(String msg) {
    14.         this.msg = msg;
    15.     }
    16. }

    7.编写配置文件:application.properties

    hello.msg=byebye

    8.HelloServiceProperties类中读取配置文件的配置数据hello.msg

    1. package com.qfedu.java2113;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. // 读取配置文件中 以 hello为前缀的值 设置到  msg
    4. @ConfigurationProperties(prefix = "hello")
    5. public class HelloServiceProperties {
    6.     private static  final String MSG = "world";
    7.     private String msg = MSG;
    8.     public String getMsg() {
    9.         return msg;
    10.     }
    11.     
    12.     public void setMsg(String msg) {
    13.         this.msg = msg;
    14.     }
    15. }

    9.完成自动配置的类HelloServiceAutoConfiguration.java

    1. package com.qfedu.java2113;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. /***
    8.  * hello-2009-starter 启动器 对应的配置 类 HelloServiceAutoConfiguration
    9.  * 每一个启动器都有一个 这样的配置类
    10.  * HelloServiceAutoConfiguration 的 作用是
    11.  * 将HelloServiceProperties 加入到容器
    12.  * 将 HelloService 加入到容器 并且和  HelloServiceProperties 进行装配
    13.  * HelloServiceAutoConfiguration 就是自动装配的配置类
    14.  * 完成了 HelloServiceProperties  和 HelloService 之间的装配
    15.  */
    16. @Configuration //标记当前类是配置类,加载 HelloServiceProperties.class配置到容器中
    17. @EnableConfigurationProperties(HelloServiceProperties.class)  // 让配置类HelloServiceProperties bean加入到容器中
    18. public class HelloServiceAutoConfiguration {
    19.     @Autowired
    20.     private HelloServiceProperties helloServiceProperties;
    21.     @Bean// 将HelloService 加入到容器
    22. //    @ConditionalOnMissingBean(HelloService.class// 只有当容器中没有 bean HelloService ,加入到容器,如果有就不需要加入到容器了
    23.     public HelloService helloService(){
    24.         HelloService helloService = new HelloService();
    25.         helloService.setMsg(helloServiceProperties.getMsg());
    26.         return helloService;
    27.     }
    28. }

    10.第五步:让自动装配类生效

    在resources 创建 resources\META-INF\spring.factories

    1. # Auto Configure  让自定义的 自动配置类生效
    2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    3. com.qfedu.java2113.HelloServiceAutoConfiguration

    11.安装到本地maven仓库

    执行 mvn install命令。

    12.第六步:在demo中引入java2113-starter 启动器

    13.第七步:编写代码,使用自定义启动器

    13.1 配置文件初始化

    13.2 使用

    1.     @Autowired
    2.     private HelloService helloService;
    3.     
    4.     @RequestMapping("/getHellMsg")
    5.     public String getHellMsg(){
    6.         return  "获取msg:"+ helloService.getMsg();
    7.     }

    14.第八步:测试看效果

    四. 总结

    哈,现在你跟着写出来了吗?这样我们就通过自定义启动器,很好地理解了SpringBoot的运行原理,特别是SpringBoot的自动装配原理。大家可以跟着辉哥上面的实现步骤,一步步地来,只要效果出来了,再逆向反推实现过程。你就会发现,原来很多所谓的实现原理也并不难

  • 相关阅读:
    Linux库的认知与使用
    阿里P8架构师仅用五步教你如何搭建SpringSecurity安全框架
    2022年武汉市小微服务业企业进入规模服务业企业奖励申报条件以及申报流程汇总
    C++基础——对于C语言缺点的补充(1)
    机器学习:人工智能中实现自动化决策与精细优化的核心驱动力
    Android JNI HIDL 简单实例学习
    小程序技术在信创操作系统中的应用趋势:适配能力有哪些?
    2345安全卫士卸载不了怎么办?
    从输入URL到展示出页面
    【考研数学】高等数学第五模块 —— 级数(2,幂级数)
  • 原文地址:https://blog.csdn.net/finally_vince/article/details/128082784