手撸一个SpringBoot的Starter,简单易上手
admin
2023-02-19 18:00:10
0次

前言:今天介绍一SpringBoot的Starter,并手写一个自己的Starter,在SpringBoot项目中,有各种的Starter提供给开发者使用,Starter则提供各种API,这样使开发SpringBoot项目变得简单。实际上Starter简单来说就是Spring+SpringMVC开发的。话不多说开始撸代码

1.创建项目

首先在idea中创建SpringBoot项目,并首先创建一个BeautyProperties类,代码代码如下:

package com.mystarter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "beauty")
public class BeautyProperties {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
  • @ConfigurationProperties(prefix = "beauty")注解表示,在resource目录下的application.properties文件中定义的变量的以beauty前缀的变量值映射到这个类中,给这个对象赋值
  • 其中这个XXProperties类,若是由阅读过SpringBoot源码的程序员都知道,在SpringBooot的源码中Starter有各种的XXProperties类与.properties文件相对应
  • 如图所示所有的自动配置相关的都在spring-boot-autoconfigure这个jar包下。其中就列举了两个RabbitProperties、BatchProperties等的配置类
    手撸一个SpringBoot的Starter,简单易上手
  • 点进RabbitProperties这个类中去看,代码如下,只粘贴一部分,这个类中也是使用@ConfigurationProperties注解将配置文件中的值与此类中的属性值相映射,目的就是为了给这些属性赋值
  • 这里留一个问题,大佬们就自己去寻找答案吧?就是那些与这些类相映射的文件在哪里呢?自行百度哈
    @ConfigurationProperties(
    prefix = "spring.rabbitmq"
    )
    public class RabbitProperties {
    private String host = "localhost";
    private int port = 5672;
    private String username = "guest";
    private String password = "guest";
    private final RabbitProperties.Ssl ssl = new RabbitProperties.Ssl();
    private String virtualHost;
    private String addresses;
  • 然后再创建一个ActionService类,这个类没什么好说的了,代码如下:
    
    package com.mystarter;

public class ActionService {

private String name;

private Integer age;

public String sayHello() {
    return "my name is "+ name +",I am "+ age +" years old";
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}


 - 最后再创建一个类ActionServiceAutoConfiguration,这个类是重点,代码如下:
 - @Configuration注解表明这是一个配置类
 - @EnableConfigurationProperties(BeautyProperties.class)表明开启@ConfigurationProperties这个注解,使这个注解生效
 - @ConditionalOnClass(ActionService.class)条件判断注解,表明有这个类ActionService,条件才生效,即配置才生效。
 - 通过@Autowired将BeautyProperties 类自动注入IOC容器中
 - @Bean将返回的值注入到容器中

package com.mystarter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration@EnableConfigurationProperties(BeautyProperties.class)
br/>@EnableConfigurationProperties(BeautyProperties.class)
public class ActionServiceAutoConfiguration {

@Autowired
BeautyProperties beautyProperties;

@Bean
ActionService helloService() {
    ActionService helloService = new ActionService();
    helloService.setName(beautyProperties.getName());
    helloService.setAge(beautyProperties.getAge());
    return helloService;
}

}


 - 然后再resources文件夹下的application.properties文件中,加入如下配置,作为使用这个Starter时候,没有设置相关值的时候作为默认值注入到配置类中

beauty.name=李依依默认
beauty.age=18


 - 最后再resources中新建一个META-INF文件夹,然后在新建一个文件spring.factories,这个名字和文件夹的名字不能改,加入配置如下,这个表明指定自动配置的类的全路径,自动配置的时候就找到这个全路径,实例化这个对象到容器中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mystarter.ActionServiceAutoConfiguration


 - 最后一步点击install,出现Build Success说明这个Starter已经安装到本地maven仓库中,可以被别人引用

![在这里插入图片描述](https://img-blog.csdnimg.cn/20191218230620617.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)
## 2.测试Starter
新建一个SpringBoot工程,在application.properties的文件中加入如下配置:

beauty.name=李依依
beauty.age=24

在pom文件中引入依赖,如下:


com.org.ldc
mystarter
1.0-SNAPSHOT


然后测试,如下代码

package com.org.ldc.mystarter;

import com.mystarter.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
class TestmystarterApplicationTests {

@Autowired
HelloService helloService;

@Test
public void contextLoads() {
    System.out.println(helloService.sayHello());
}

}


执行测试,出现如下,说明创建成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019121823094633.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)
>更多的教程请关注:非科班的科班,路过有空的大佬们点个赞,谢谢大家

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...