9、手写一个starter

一、starte详解:

1、starter场景启动器:

SpringBoot-starter是一个集成接合器,主要完成两件事:

(1)、引入模块所需的相关jar包

(2)、自动配置各自模块所需的属性

注:

spring-boot-starter-*:官方提供

*-spring-boot-starter:第三方提供

2、starter实现原理:

(1)、首先,SpringBoot在启动时会先去依赖的starter包中寻找 resources/META-INF/spring.factories 文件,然后根据文件中配置的Jar包去扫描项目所依赖的Jar包

(2)、然后,根据 spring.factories 配置加载自动配置类。

(3)、最后,根据 @Conditional 注解的条件,进行自动配置,并将Bean注入Spring Context上下文当中。

二、Springboot自动配置原理:

详细参考

Springboot启动的时候会通过@EnableAutoConfiguration注解找到配置文件中的所有自动配置类(XxxxAutoConfiguration类),并对其进行加载,而这些自动配置类都是以AutoConfiguration结尾来命名的,它实际上就是一个JavaConfig形式的Spring容器配置类,它能通过以Properties结尾命名的类中取得在全局配置文件中配置的属性如:server.port,而XxxxProperties类是通过@ConfigurationProperties注解与全局配置文件中对应的属性进行绑定的。

三、手写一个starter:

手写一个starter。功能很简单,调用starter内对象的一个方法输出”xxx,hello! xxx”

1、hello-spring-boot-starter工程:

(1)、改POM:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

        org.springframework.boot
        spring-boot-starter-parent
        2.7.1

    com.iven
    hello-spring-boot-starter
    0.0.1-SNAPSHOT
    hello-spring-boot-starter
    Demo project for Spring Boot

        1.8

            org.springframework.boot
            spring-boot-starter

            org.springframework.boot
            spring-boot-starter-web

            org.springframework.boot
            spring-boot-configuration-processor
            true

            org.springframework.boot
            spring-boot-autoconfigure

            org.projectlombok
            lombok
            1.18.20

                org.apache.maven.plugins
                maven-compiler-plugin
                3.10.1

                    8
                    8

(2)、编写业务类Service:

@Data
@AllArgsConstructor  //全参构造
@NoArgsConstructor  //无参构造
public class HelloService {
    private  String name;
    private String detail;

    public String hello() {
        return getName()+",hello! " + getDetail();
    }

}

(3)、编写Configuration配置类(主):

@Configuration    //表明这是一个springBoot的配置类
public class HelloConfig {
    @Value("${com.hello:Iven}")
    private  String name;

    @Value("${com.hello:冲冲冲}")
    private String detail;

    @Bean    //将对象放入IOC容器中,对象名就是方法名
    public HelloService helloService(){
        return new HelloService(name,detail);
    }

}

(4)、编写spring.factories文件:

为了让springboot容器扫描到配置类,建一个resource目录,一个META-INF文件夹和spring.factories文件

9、手写一个starter
#等号后面是配置类的全路径(包+配置类名)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.iven.hellospringbootstarter.config.HelloConfig

(5)、install打包到本地仓库;

2、test-starter-demo工程:

(1)、添加依赖:

com.iven
    hello-spring-boot-starter
    0.0.1-SNAPSHOT

(2)、使用:

@RestController
@RequestMapping("/v1")
public class control {
    @Autowired
    private HelloService helloService;

    @RequestMapping("")
    public String demo(){
        helloService.setName("Pitt");
        return helloService.hello();
    }
}

搜索

复制

Original: https://www.cnblogs.com/Iven-L/p/16592159.html
Author: 爱文(Iven)
Title: 9、手写一个starter

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/575691/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • MyBatis-Plus入门教程及基本API使用案例

    一、MyBatisPlus简介 1. 入门案例 问题导入 MyBatisPlus环境搭建的步骤? 1.1 SpringBoot整合MyBatisPlus入门程序 ①:创建新模块,选…

    数据库 2023年5月24日
    0103
  • 必应咋想的

    首页里弄了个阴森的图片,下面有个山洞,里面有白衣女鬼飘过,还有背景音乐。 看右下角的介绍里有名字叫:万圣节之夜在黑暗树篱 Original: https://www.cnblogs…

    数据库 2023年6月9日
    0102
  • 我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知

    面试官: 我看你的简历上写着 精通MySQL,问你个简单的问题, MySQL联合索引有什么特性? 心想,这还不简单,这不是问到我手心里了吗?听我给你背一遍八股文! 我: MySQL…

    数据库 2023年5月24日
    081
  • mysql使用存储过程批量给表加字段

    背景:在一个项目中,您需要将相同的字段添加到数百个表中,这很难手动添加,因此您计划使用存储过程来实现。 [En] Background: in a project, you nee…

    数据库 2023年5月24日
    0120
  • 删除MySQL数据用户

    mysql删除用户的方法: 1、使用”drop user 用户名;”命令删除; 2、使用”delete from user where user…

    数据库 2023年6月14日
    086
  • Android Studio的初次认识

    Android的初试 一、认识Android Studio 在我们新建项目的时候,会遇到这样的一个窗口,首先我们认识一下这些都是什么,这样我们才能够更好的进行下一步的学习! 这里的…

    数据库 2023年6月11日
    0127
  • Secret加密以及Configmapd配置介绍

    今天我们来了解有关Secret加密以及Configmapd配置介绍 一、Configmapd配置介绍 ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程…

    数据库 2023年6月14日
    069
  • LRU算法

    class LRUCahce { private Node head; private Node tail; private Map hashMap; private int ca…

    数据库 2023年6月14日
    081
  • git拉项目出现的小问题

    问题描述 在IDEA中拉代码事报错。 点击查看报错信息 error: unable to read askpass response from ‘C:\Users\&#x9…

    数据库 2023年6月11日
    0109
  • Java学习-第一部分-第二阶段-第四节:常用类

    常用类 笔记目录:(https://www.cnblogs.com/wenjie2000/p/16378441.html) 包装类 包装类的分类 针对八种基本数据类型相应的引用类型…

    数据库 2023年6月11日
    0109
  • Linux 实用命令

    实用命令: 1、查看端口占用情况 查看该端口是&…

    数据库 2023年6月16日
    079
  • 建议收藏备查!MySQL 常见错误代码说明

    先给大家看几个实例的错误分析与解决方案。 1.ERROR 2002 (HY000): Can’t connect to local MySQL server throu…

    数据库 2023年6月9日
    0134
  • 9.回文数

    给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 例如,121 是回文,而…

    数据库 2023年6月16日
    069
  • Celery异步任务

    (1)安装celery pip install celery==4.2.1 (2)celery使用 在项目适当位置创建celery_tasks目录用于保存celery异步任务。 在…

    数据库 2023年6月14日
    081
  • 如何构建你自己的计算机网络知识体系?

    大家好,我是小牛肉,不知道各位曾经有没有和我一样的困惑,就是有些知识好像已经看了好多遍了,但是知识点在脑子中是分散的,没办法串联起来,别人问一个问题我能答出来一点,但是你让我自己从…

    数据库 2023年6月6日
    086
  • ES6 Map映射

    ES6 Map映射 Map对象保存的是 键值对。任何类型值(对象或原始值)都可以作为一个键或一个值。 基础用法 //创建 let map = new Map(); //增|改,返回…

    数据库 2023年6月11日
    099
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球