手撸一个SpringBoot的Starter,简单易上手

前言:今天介绍一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等的配置类
    在这里插入图片描述在这里插入图片描述
  • 点进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)
@ConditionalOnClass(ActionService.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仓库中,可以被别人引用

在这里插入图片描述在这里插入图片描述

2.测试Starter

新建一个SpringBoot工程,在application.properties的文件中加入如下配置:

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

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

<dependency>
    <groupId>com.org.ldc</groupId>
    <artifactId>mystarter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

然后测试,如下代码

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());
    }

}

执行测试,出现如下,说明创建成功

在这里插入图片描述在这里插入图片描述

更多的教程请关注:非科班的科班,路过有空的大佬们点个赞,谢谢大家

猜你喜欢

转载自www.cnblogs.com/liduchang/p/12064560.html