【Spring Boot】自定义启动器starter

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 【Spring Boot】自定义启动器starter,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

一、背景

虽然Spring官方给我们提供了很多的启动器供我们使用
但有时候我们也会遇到某些特殊场景,这些启动器满足不了
这个时候就需要自定义一个启动器供我们使用

二、自定义启动器

在之前学习Spring Boot的过程中,我们已经对启动器有了一个大致的了解

Spring Boot实现某个功能,一般是引入对应场景的启动器(一般不写代码,只是声明这个启动器需要引用哪些依赖),然后这个启动器又有对应的自动配置包

1、创建一个启动器的自动配置模块

先写和配置文件中配置项互相绑定的实体类

package com.decade.autoConfigure.pojo;

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

// 将这个类的属性和系统配置文件中的属性进行绑定
@ConfigurationProperties(prefix = "team")
public class TeamInfo {
    private String winnerName;

    private String loserName;

    public String getWinnerName() {
        return winnerName;
    }

    public void setWinnerName(String winnerName) {
        this.winnerName = winnerName;
    }

    public String getLoserName() {
        return loserName;
    }

    public void setLoserName(String loserName) {
        this.loserName = loserName;
    }
}

再写自己要实现的业务逻辑

package com.decade.autoConfigure.service;

import com.decade.autofigure.pojo.TeamInfo;
import org.springframework.beans.factory.annotation.Autowired;

public class TestService {

	// 引入和yaml文件中配置项绑定的类
    @Autowired
    private TeamInfo teamInfo;

    public String testMethod() {
        return teamInfo.getWinnerName() + "今天早上,绝杀了" + teamInfo.getLoserName();
    }
}

接着,写一个自动配置类,向容器中放入组件

package com.decade.autoConfigure.auto;

import com.decade.autofigure.pojo.TeamInfo;
import com.decade.autofigure.service.TestService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 使得TeamInfo中的@ConfigurationProperties注解生效,将TeamInfo注册到容器中
@EnableConfigurationProperties(TeamInfo.class)
@Configuration
public class AutoConfiguration {

    @Bean
    // 只有容器中不存在TestService这个类型的bean时,才回去初始化这个bean
    @ConditionalOnMissingBean(TestService.class)
    public TestService testService() {
        TestService testService = new TestService();
        return testService;
    }
}

最后,在新版本的Spring Boot中为了确保框架启动时加载该配置类
我们需要在这个模块的resource文件下
新建/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
然后在里面写自己之前创建的自动配置类

注意:老版可能是放在META-INF/spring.factories文件中

com.decade.autoConfigure.auto.AutoConfiguration

2、创建一个启动器模块

不用写任何业务代码,只需要在pom文件中,引用之前的创建的自动配置模块
在这里插入图片描述

3、在业务模块中引入启动器

如图,引入我们自己定义的启动器
在这里插入图片描述
然后在yaml文件中设置好绑定的配置项

再写一个测试方法进行测试即可

package com.decade;

import com.decade.autoConfigure.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
class SpringbootWebApplicationTests {

    @Autowired
    private TestService testService;

    @Test
    public void testCustomStarter() {
        System.out.println(testService.testMethod());
    }
}

测试方法结果如下图
在这里插入图片描述

如有错误,欢迎指正!!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/136737.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!