1 自定义starter的原理

  • SpringBoot自定义starters - 图1这个场景需要使用的依赖是什么?
  • SpringBoot自定义starters - 图2如何编写自动配置。
  1. @Configuration //指定这个类是配置
  2. @ConditionalOnXxx //在指定条件成立的情况下自动配置类生成
  3. @AutoConfigureAfter //指定自动配置类的顺序
  4. @Bean //给容器中添加组件
  5. @ConfigurationProperties结合相关的XxxxProperties类来绑定相关的配置
  6. @EnableConfigurationProperties //让XxxProperties类生效,并加入到容器中
  7. 自动配置类要能加载,需要将其配置到META-INF/spring.factories
  8. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  9. org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
  10. org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  11. org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
  12. org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
  13. ...
  • SpringBoot自定义starters - 图3启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。

启动器原理.png

2 自定义starter

2.1 使用IDEA新建一个空的工程spring-boot-starter

新建一个空的工程spring-boot-starter.png

2.2 新建sunxiaping-spring-boot-starter-autoconfigurer工程

  • 导入相关jar的Maven坐标:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.sunxiaping.starter</groupId>
  12. <artifactId>sunxiaping-spring-boot-starter-autoconfigurer</artifactId>
  13. <version>0.0.1</version>
  14. <name>sunxiaping-spring-boot-starter-autoconfigurer</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>14</java.version>
  18. </properties>
  19. <dependencies>
  20. <!-- 引入spring-boot-starter -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter</artifactId>
  24. </dependency>
  25. </dependencies>
  26. </project>
  • 新建HelloProperties.java
  1. package com.sunxiaping.starter;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties(prefix = "sunxiaping.hello")
  4. public class HelloProperties {
  5. private String prefix;
  6. private String suffix;
  7. public String getPrefix() {
  8. return prefix;
  9. }
  10. public void setPrefix(String prefix) {
  11. this.prefix = prefix;
  12. }
  13. public String getSuffix() {
  14. return suffix;
  15. }
  16. public void setSuffix(String suffix) {
  17. this.suffix = suffix;
  18. }
  19. }
  • 新建HelloService.java
  1. package com.sunxiaping.starter;
  2. public class HelloService {
  3. private HelloProperties helloProperties;
  4. public HelloProperties getHelloProperties() {
  5. return helloProperties;
  6. }
  7. public void setHelloProperties(HelloProperties helloProperties) {
  8. this.helloProperties = helloProperties;
  9. }
  10. public String sayHello(String name) {
  11. return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
  12. }
  13. }
  • 新建HelloServiceAutoConfiguration.java
  1. package com.sunxiaping.starter;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. @ConditionalOnWebApplication
  9. @EnableConfigurationProperties(value = HelloProperties.class)
  10. public class HelloServiceAutoConfiguration {
  11. @Autowired
  12. private HelloProperties helloProperties;
  13. @Bean
  14. public HelloService helloService(){
  15. HelloService helloService = new HelloService();
  16. helloService.setHelloProperties(helloProperties);
  17. return helloService;
  18. }
  19. }
  • 在resource目录下新建META-INF目录,并在其新建spring.factories文件
  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.sunxiaping.starter.HelloServiceAutoConfiguration

2.3 新建sunxiaping-spring-boot-starter工程

  • 新建相关jar包的Maven坐标:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.sunxiaping.starter</groupId>
  7. <artifactId>sunxiaping-spring-boot-starter</artifactId>
  8. <version>1.0</version>
  9. <!-- 启动器 -->
  10. <dependencies>
  11. <!-- 引入自动配置模块 -->
  12. <dependency>
  13. <groupId>com.sunxiaping.starter</groupId>
  14. <artifactId>sunxiaping-spring-boot-starter-autoconfigurer</artifactId>
  15. <version>0.0.1</version>
  16. </dependency>
  17. </dependencies>
  18. </project>