写于:2018-12-21 12:11:30

一、什么是 spring boot starter?

其实 spring boot starter 就是我们项目中的某一模块,比如:web 依赖的 starter ,就是将所有web开发需要用到的 JAR 包整合,并进行相关自动化配置设定。
而区别 模块 和 starter 的重点在于:starter 提供了自动化配置功能。

《Springboot-自动配置》

二、自定义 spring boot starter

自定义 spring boot starter 模拟生成支付宝订单 其中:模拟生成支付宝订单中只是读取配置的 appid 和 secrect 进行输出。

项目结构如下
01.png

step1、创建 spring boot 项目 my-starter

step2、pom 文件追加依赖

  1. <!-- springframework 相关注解 -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. </dependency>
  6. <!-- @Conditionxx 相关注解 -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-autoconfigure</artifactId>
  10. </dependency>
  11. <!-- @ConfigurationProperties 注解 读取配置 -->
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-configuration-processor</artifactId>
  15. <optional>true</optional>
  16. </dependency>
  17. <!-- 自动生成 getter setter 等 -->
  18. <dependency>
  19. <groupId>org.projectlombok</groupId>
  20. <artifactId>lombok</artifactId>
  21. <scope>provided</scope>
  22. </dependency>

step3、创建 配置属性类 AlipayProperties.class

  1. /**
  2. * <p> 支付宝配置文件 </p>
  3. *
  4. * @author WTF名字好难取
  5. */
  6. @Getter
  7. @Setter
  8. @ToString
  9. @ConfigurationProperties(prefix = "alipay")
  10. public class AlipayProperties {
  11. /** 模拟:appid接收 **/
  12. private String appId;
  13. /** 模拟:appSecrect接收 **/
  14. private String secrect;
  15. }

step4、创建 alipay 服务类提供生成订单服务 AlipayService.class

  1. /**
  2. * <p> 支付宝 服务类 </p>
  3. *
  4. * @author WTF名字好难取
  5. */
  6. @Getter
  7. @Setter
  8. @ToString
  9. @AllArgsConstructor
  10. public class AlipayService {
  11. private AlipayProperties alipayProperties;
  12. /**
  13. * <p> 模拟生成支付宝订单 </p>
  14. * @return
  15. */
  16. public String generatorOrder(){
  17. return alipayProperties.getAppId() + "," + alipayProperties.getSecrect();
  18. }
  19. }

step5、创建 自动配置类 AlipayAutoConfiguration.class

  1. /**
  2. * <p> 支付宝自动配置类 </p>
  3. *
  4. * @author WTF名字好难取
  5. */
  6. @Configuration
  7. /** 标识为配置类 **/
  8. @EnableConfigurationProperties(value = {AlipayProperties.class}) /** 需要启动的配置文件 **/
  9. @ConditionalOnClass(AlipayService.class) /** 当 AlipayService 存在时,该配置类才有效 **/
  10. @ConditionalOnProperty(prefix = "alipay", value = "enable", matchIfMissing = true)
  11. public class AlipayAutoConfiguration {
  12. @Autowired
  13. private AlipayProperties alipayProperties;
  14. @Bean
  15. @ConditionalOnClass(AlipayService.class)
  16. public AlipayService alipayService(){
  17. AlipayService alipayService = new AlipayService(alipayProperties);
  18. return alipayService;
  19. }
  20. }

step6、创建 /resource/META-INF/spring.factories 并追加 自动配置信息

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.qguofeng.pay.alipay.AlipayAutoConfiguration

三、使用自定义的 starter

项目结构
02.png

step1、创建 Spring Boot 项目: common-project

step2、pom 文件追加依赖

  1. <!-- web 依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- 自定义 starter 依赖 -->
  7. <dependency>
  8. <groupId>com.qguofeng</groupId>
  9. <artifactId>my-starter</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </dependency>

step3、启动类 Application.java 追加测试 API

  1. @SpringBootApplication
  2. @RestController
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. @Autowired
  8. private AlipayService alipayService;
  9. /** web 服务测试 **/
  10. @RequestMapping("/alipay")
  11. public String process(){
  12. return alipayService.generatorOrder();
  13. }
  14. }

step4、配置文件 application.properties 增加配置

  1. spring.application.name=common-project
  2. server.port = 9528
  3. ## 自定义 ali配置 (对应 starter 中的配置属性)
  4. alipay.appId = AppID
  5. alipay.secrect = SECRECT

step5、访问测试 API http://localhost:9528/alipay

  1. $ curl http://localhost:9528/alipay
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 13 100 13 0 0 76 0 --:--:-- --:--:-- --:--:-- 76
  5. AppID,SECRECT