TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码

徒手撸一个Spring Boot Starter,学习自动化配置

Laughing博主
2020-09-26
/
0 评论
/
1,086 阅读
/
859 个字
/
百度已收录
09/26
本文最后更新于2024年03月21日,已超过183天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

what-are-spring-boot-starter-jars(spring boot中的starter是干啥的)

Spring Boot Starter是在SpringBoot组件中被提出来的一种概念,stackoverflow上面已经有人概括了这个starter是什么东西,想看完整的回答戳这里

下面摘抄一段

Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go。

扔到百度翻译里面翻译下:

starter pom是一组方便的依赖描述符,可以包含在应用程序中。您可以获得所需的所有Spring和相关技术的一站式服务,而不必搜索示例代码和复制粘贴大量依赖描述符。例如,如果您想开始使用Spring和JPA进行数据库访问,只需在项目中包含springbootstarterdatajpa依赖项,就可以开始了。

其实说白了,就是把一些重复的、机械的工作合成起来。

如何自定义自己的starter

其实starter也只是一个普通的maven工程,关于starter工程的命令,可以参考官方文档


创建一个starter基本包含以下几个步骤:

  1. 创建一个maven项目,关于项目的命名你可以参考考官方文档
  2. 创建一个ConfigurationProperties用于保存你的配置信息
  3. 增加starter实现类
  4. 创建一个AutoConfiguration,引用定义好的配置信息
  5. 把AutoConfiguration类加入spring.factories配置文件中进行声明
  6. 打包项目并推送到maven仓库
  7. 新建SpringBoot项目引入starter依赖并使用。

    下面的演示,我们创建一个starter,能够根据配置的url,打印并输出对应页面的内容,同时如果不传递参数,自动打印香草物语https://www.xiangcaowuyu.net的首页


第一步创建工程并修改对应的xml文件

创建工程后,需要修改对应的pom.xml文件,并增加依赖,具体修改内容如下:

注意这里面的几个坑

  1. 自定义的starter是不能有启动入口的!即:只能作为工具类!所以我们必须把启动类删除掉。否者,在依赖的工程里面无法引入类。
  2. 需要把pom.xmlbuild节点全部删掉,不然无法install或者deploy

创建一个ConfigurationProperties用于保存你的配置信息

先修改application.yaml文件,增加默认配置

blog:
  url: https://www.xiangcaowuyu.net
  1. 增加配置类,并注入配置文件

    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    @ConfigurationProperties(prefix = "blog")
    public class BlogPropertes {
    
        private String url;
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
    }

增加starter实现类

我的实现类只是简单的通过jsoup打印并返回一个html的内容。

**
 * @author laughing
 * @date 2020/9/26
 * @site https://www.xiangcaowuyu.net
 */
public class PrintHtmlService {

    public void setUrl(String url) {
        this.url = url;
    }

    private String url;

    /**
     * 打印并返回html内容
     * @return
     * @throws IOException
     */
    public String print() throws IOException {
        Document document = Jsoup.parse(new URL(this.url), 300 * 1000);
        String htmlString = document.html();
        System.out.println(htmlString);
        return htmlString;
    }

}

创建一个AutoConfiguration,引用定义好的配置信息

/**
 * @author laughing
 * @date 2020/9/26
 * @site https://www.xiangcaowuyu.net
 */
@Configuration
@EnableConfigurationProperties(BlogPropertes.class)
@ConditionalOnClass(PrintHtmlService.class)
public class AutoConfiguration {

    @Resource
    BlogPropertes blogPropertes;

    @Bean
    @ConditionalOnClass
    public PrintHtmlService printHtmlService(){
        PrintHtmlService printHtmlService = new PrintHtmlService();
        printHtmlService.setUrl(blogPropertes.getUrl());
        return printHtmlService;
    }

}

@Configuration

标识本类是配置类(相当于spring中application.xml)

@EnableConfigurationProperties(AutoConfigruationProperties.class)

如果AutoConfigruationProperties中有注解@ConfigurationProperties 那么这个类就
会被加到spring上下文的容器中,也就是可以通过@Resource来注入

@ConditionalOnClass

当类路径下有指定类的情况下 才进行下一步

@ConditionalOnMissingBean

当spring容器中没有这个Bean的时候才进行下一步

把AutoConfiguration类加入spring.factories配置文件中进行声明

  1. 在resources文件夹增加META-INF
  2. 在META-INF文件夹下增加spring.factories文件,并设置自动装配
org.springframework.boot.autoconfigure.EnableAutoConfiguration = org.lisen.printhtmlstarter.config.AutoConfiguration

打包项目并推送到maven仓库

如果有maven私服,可以推送到远程仓库,我这里只是自己测试,所以直接install到自己本地仓库

mvn package install

新建SpringBoot项目引入starter依赖并使用

我这里直接新建一个普通的web项目,加入pringt-html-starter依赖进行测试

新建项目并加入依赖

第一次我们先不重写默认的url

新建一个测试类,代码如下:

/**
 * @author laughing
 * @date 2020/9/26
 * @site https://www.xiangcaowuyu.net
 */
@RestController
public class TestController {

    @Resource
    PrintHtmlService printHtmlService;

    @RequestMapping("/print")
    public String print() throws IOException {
        return printHtmlService.print();
    }

}

通过测试,我们可以发现,正常输入了默认的https://www.xiangcaowuyu.net的内容

第二次我们修改配置文件,重写url为http://www.baidu.com

修改配置文件如下,

blog:
  url: https://www.baidu.com

再次请求,我们发现,正常输出了百度的首页

Spring Boot
朗读
赞(0)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

香草物语

本文链接:

https://www.xiangcaowuyu.net/java/use-a-spring-boot-starter-to-learn-automatic-configuration.html(转载时请注明本文出处及文章链接)

评论 (0)
  1. wendao 闲逛
    MacOS · Google Chrome

    多谢谢!

    2019-04-03 回复
  2. bevin 闲逛
    MacOS · Google Chrome

    试一下

    2018-08-30 回复