资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

手撸一个SpringBoot的Starter,简单易上手-创新互联

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

创新互联建站成立以来不断整合自身及行业资源、不断突破观念以使企业策略得到完善和成熟,建立了一套“以技术为基点,以客户需求中心、市场为导向”的快速反应体系。对公司的主营项目,如中高端企业网站企划 / 设计、行业 / 企业门户设计推广、行业门户平台运营、手机APP定制开发移动网站建设、微信网站制作、软件开发、成都服务器托管等实行标准化操作,让客户可以直观的预知到从创新互联建站可以获得的服务效果。

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)
>更多的教程请关注:非科班的科班,路过有空的大佬们点个赞,谢谢大家

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:手撸一个SpringBoot的Starter,简单易上手-创新互联
文章源于:http://cdkjz.cn/article/ggsho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220