了解Spring starter,明白spring boot的核心

Spring boot之所以流行,是因为Spring starter。Spring starter是Spring boot的核心,可以理解为一个可拔插式的插件,例如,你想使用Reids插件,那么可以使用spring-boot-starter-redis;如果想使用MongoDB,可以使用spring-boot-starter-data-mongodb。

        如果我要使用redis,我直接引入redis驱动jar包就行了,何必要引入starter包?starter和普通jar包的区别在于,它能够实现自动配置,和Spring Boot无缝衔接,从而节省我们大量开发时间。下面通过制作一个Starter的jar包来了解Spring starter是如何让Spring的开发更加简单方便的。

1,Starter的命名

        官方对Starter项目的jar包定义的 artifactId 是有要求的,当然也可以不遵守。Spring官方Starter通常命名为spring-boot-starter-{name}如:spring-boot-starter-web,Spring官方建议非官方的starter命名应遵守{name}-spring-boot-starter的格式。

2,Maven依赖

        Spring starter让spring boot简单,主要是引入了自动配置,所以这里需要引入自动配置相关的依赖。

<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-configuration-processor</artifactId>

        <optional>true</optional>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-autoconfigure</artifactId>

    </dependency>

</dependencies>

        其中 spring-boot-configuration-processor 的作用是编译时生成 spring-configuration-metadata.json ,此文件主要给IDE使用。如当配置此jar相关配置属性在 application.properties或者applicationg.yml ,你可以用ctlr+鼠标左键点击属性名,IDE会跳转到你配置此属性的类中。

3,编写Service类

        这里讲一下我们的Starter要实现的功能,很简单,提供一个Service,包含一个能够将配置文件中配置的字符串根据传入的字符进行分割的方法String[] split(String separatorChar)。

public class StarterService {

    private String config;

    public StarterService(String config) {

        this.config = config;

    }

    public String[] split(String separatorChar) {

        return StringUtils.split(this.config, separatorChar);

    }

4,编写配置文件读取类

        配置文件读取类一般命名为XxxProperties,该配置类主要用于接收Spring boot中application.properties或者application.yml的配置项,主要代码如下:

@ConfigurationProperties(prefix = "starter.test")

public class StarterTestProperties {

  private String config; 

    public void setConfig(String config) {

        this.config = config;

    }

    public String getConfig() {

        return config;

    }

}

5,编写AutoConfigure类(关键)

@Configuration

@ConditionalOnClass(StarterService.class)

@EnableConfigurationProperties(StarterTestProperties.class)

public class StarterAutoConfigure {

    @Autowired

    private StarterTestProperties properties;

    @Bean

    @ConditionalOnMissingBean

    @ConditionalOnProperty(prefix = "example.service", value = "enabled", havingValue = "true")

    StarterService starterService (){

        return new StarterService(properties.getConfig());

    }

}

说明以下代码中的几个注解:

@ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。

@ConditionalOnMissingBean,当Spring 容器中不存在该Bean时创建该bean。

@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true"),当配置文件中example.service.enabled=true时,创建该bean。

6,创建spring.factories

        在resources/META-INF/下创建spring.factories文件,并添加如下内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.StarterAutoConfigure

7,编译jar包

        将maven工程编译成jar包。

8,创建spring boot工程,引入jar包

        创建spring boot工程,并引入刚才生成的starter jar包。

9,测试

在application.properties 配置文件中添加配置信息:

example.service.enabled=true

starter.test=yyyy,xxx,lll,zzz

单元测试用例如下:

@Autowired

private StarterService starterService;

@Test

public void starterTest() {

    String[] splitArray = starterService.split(",");

    System.out.println(splitArray);

}

    一个简单的Starter就创建完成了,从一个简单的starter创建过程,就可以看出spring boot是如何简化我们的项目开发的。

原文地址:https://www.cnblogs.com/zhuyeshen/p/12525431.html