概述
众所周知,Spring Boot的核心理念是“约定高于配置”,这一理念最终落地就是通过Starter模块来实现的。个人理解各种Starter模块最主要作用包括3部分:依赖管理、约定配置和自动装配。
- 依赖:每个Starter会将它所需要的依赖都引入,使用该Starter的项目无需关注,不过对于不同Starter依赖了同一依赖包的不同版本时,还是需要关注一些依赖冲突的。
- 约定配置:在Starter当中,会定义它需要使用的配置项,同时这些配置一般都会有约定的默认值。如果你都按约定俗成的配置,你使用某个Starter时甚至你项目中无需增加任何配置。
- 自动装配:在Starter中,都会有若干AutoConfiguration类,这些类会通过定义的配置进行一些初始化工作,可能是初始化一些Bean,也可能是执行一些业务逻辑。
除了使用官方提供的Starter外,也可以实现自定义的Starter,比如说将一些公共模块通过封装成Spring Boot的Starter,其它项目使用是就会更加方便。
自定义Starter
创建项目
创建一个Gradle(本文使用gradle进行项目管理)项目custom-spring-boot-starter,引入以下依赖:
dependencies { compile ( "org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE", "org.springframework.boot:spring-boot-configuration-processor:2.0.5.RELEASE" )}
按照Spring官方的建议,Starter的命名规则如下:
- 官方:spring-boot-starter-{moduleName}
- 第三方:{moduleName}-spring-boot-starter
==既然使用类Spring Boot,就要遵循“约定高于配置”的思想,Starter模块的命名建议还是按照约定来吧 ^^_^^!==
配置类
创建一个类 CustomProp.class,这是一个典型JavaBean。然后通过 @ConfigurationProperties 注解指定这是一个配置类,并设置注解的 prefix 属性值指定配置项的前缀。
package org.cent.starter.custom.prop;import lombok.Getter;import lombok.Setter;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.NestedConfigurationProperty;/** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/1/4. * @description: 自定义Starter配置类 */@ConfigurationProperties(prefix = "org.cent.stater.custom")@Setter@Getterpublic class CustomProp { private String name = "cent"; @NestedConfigurationProperty private String description = "天气真好";}
业务Bean
- 创建一个接口类 CustomService.class ,定义一个示例的业务方法 sayHello()。
package org.cent.starter.custom.service;/** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/1/4. * @description: 示例业务接口 */public interface CustomService { /** * 示例方法 */ void sayHello();}
- 创建一个类 CustomServiceImpl.class 实现上一步骤创建的接口,这个实现类中使用到上面的配置类。
package org.cent.starter.custom.service.impl;import lombok.AllArgsConstructor;import org.cent.starter.custom.prop.CustomProp;import org.cent.starter.custom.service.CustomService;/** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/1/4. * @description: 示例业务接口实现类 */@AllArgsConstructorpublic class CustomServiceImpl implements CustomService { private CustomProp customProp; @Override public void sayHello() { String message = String.format("你好!%s,%s!", customProp.getName(), customProp.getDescription()); System.out.println(message); }}
自动化配置
- 创建一个自动化配置类 CustomAutoConfiguration.class:
- 通过 @Configuration 注解指明这是一个JavaConfig。
- 通过 @EnableConfigurationProperties 注解启用上面定义的配置类,这样这个配置类才会被Spring容器自动装配。
package org.cent.starter.custom.autoconfig;import org.cent.starter.custom.prop.CustomProp;import org.cent.starter.custom.service.CustomService;import org.cent.starter.custom.service.impl.CustomServiceImpl;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/1/4. * @description: 自动配置类 */@Configuration@EnableConfigurationProperties(CustomProp.class)public class CustomAutoConfiguration { /** * 初始化自定义Starter的Bean * * @param customProp * @return */ @Bean public CustomService customService(CustomProp customProp) { return new CustomServiceImpl(customProp); }}
- 在 src/main/resource/META-INF目录下创建一个配置文件 spring.factories,配置文件内容见下文。这个文件很重要,spring-core中的SpringFactoriesLoader通过检索这个文件中的内容,获取到指定的配置类。
# 自动初始化配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.cent.starter.custom.autoconfig.CustomAutoConfiguration
大功告成
至此,一个典型的自定义Starter就开发完成了,项目结构如下:
使用示例
创建示例项目
- 创建一个示例项目 custom-starter-use-example,在项目依赖中引入上面开发的自定义Stater。
dependencies { compile "org.springframework.boot:spring-boot-starter-web:${springBootVersion}" compile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}" compile project (":custom-spring-boot-starter")}
项目启动类
给项目一个启动类,Spring Boot项目的启动类,这里就不说明了。
package org.cent.custom.starter.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/1/4. * @description: 示例启动类 */@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
测试
- 编写测试用例,使用自定义Starter里面初始化的业务Bean,代码如下:
package org.cent.example.test;import org.cent.custom.starter.example.Application;import org.cent.starter.custom.service.CustomService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/1/4. * @description: */@RunWith(SpringRunner.class)@SpringBootTest(classes = {Application.class})public class CustomServiceTest { @Autowired private CustomService customService; @Test public void sayHello(){ customService.sayHello(); }}
-
测试,使用默认配置。 使用默认配置时,测试结果如下:
-
测试,使用自定义配置
在test的resources目录下,增加一个application.yml配置文件,文件配置如下:
org: cent: stater: custom: name: jack description: 这是使用自定义配置的
- 运行测试用例,结果如下:
示例代码
码云: