SpringBoot自定义启动器Starter流程详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot自定义启动器Starter流程详解

Decade0712   2022-11-16 我要评论

一、背景

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

测试方法结果如下图

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们