Spring Boot自定义Starter编写Demo

Spring Boot的自定义Starter

​ 一般创建自定义Starter时,会分为两个项目:xxx-spring-boot-starter, xxx-spring-boot-autoconfigure。starter模块不写任何业务代码,只用来引入autoconfigure模块的依赖,而在其他项目引入该自定义starter时,直接引用starter模块的依赖即可。

​ 下面是编写自定义starter的流程:

  1. 创建两个模块:

    这里以example-spring-boot-starterexample-spring-boot-autoconfigure为例,直接创建Maven项目即可。

  2. 编写starter模块的配置

    starter模块的pom文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <artifactId>example-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
        <groupId>org.example</groupId>
        <packaging>jar</packaging>
        <name>example-spring-boot-starter</name>
        
        <dependencies>
            <!--starter模块只需要导入autoconfigure模块的依赖即可-->
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>example-spring-boot-autoconfigure</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </project>
    
  3. 编写autoconfigure模块的配置

    autoconfigure模块的pom文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <groupId>org.example</groupId>
        <artifactId>example-spring-boot-autoconfigure</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.1.12.RELEASE</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        
        <dependencies>
            <!-- springboot starter依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!-- 引入该依赖后,可以在编写配置文件时进行提示 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </project>
    
  4. 在autoconfigure模块编写对应的具体代码:

    1. ExampleAutoConfigure(配置类)

      @Configuration	// 标注配置类
      @EnableConfigurationProperties(ExampleProperties.class)	// 在有配置时生效
      @ConditionalOnProperty(	// 在配置了org.example.enabled属性为true时,配置类才会生效
              prefix = "org.example",
              name = "enabled",
              havingValue = "true"
      )
      public class ExampleAutoConfigure {
          @Autowired
          private ExampleProperties exampleProperties;
      
          // 当容器中没有ExampleService时,注入
          @ConditionalOnMissingBean(ExampleService.class)
          @Bean
          public ExampleService exampleService() {
              return new ExampleService(exampleProperties);
          }
      }
      
    2. ExampleProperties(配置文件对应)

      @ConfigurationProperties(prefix = "org.example")	// 设置配置文件的前缀
      public class ExampleProperties {
          private String name;
          private String content;
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public String getContent() {
              return content;
          }
      
          public void setContent(String content) {
              this.content = content;
          }
      }
      
      
    3. ExampleService(业务类)

      public class ExampleService {
          private ExampleProperties exampleProperties;
      
          public ExampleService() {
      
          }
      
          public ExampleService(ExampleProperties exampleProperties) {
              this.exampleProperties = exampleProperties;
          }
      
          public String testExample() {
              return "ExampleService : " + exampleProperties.getName() + " -- " + exampleProperties.getContent();
          }
      }
      
  5. 编写spring.factories文件:

    注意,这个文件必须在autoconfigure模块的resources/META-INF/spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.config.ExampleAutoConfigure
    
  6. 在其他项目引入进行测试

    在其他项目中引入时,只需要直接引入example-spring-boot-starter依赖,在配置文件进行对应的配置后,就可以使用了。

    pom文件:

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>example-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    

    测试接口:

    @RestController
    public class TestController {
        @Autowired
        private ExampleService exampleService;
    
        @GetMapping("/print")
        public String print() {
            System.out.println("..........");
    
            return exampleService.testExample();
        }
    }
    

    配置文件application.yml

    server:
      port: 7888
    org:
      example:
        enabled: true
        name: sprog
        content: 你好世界
    
  7. 访问接口的结果:

在这里插入图片描述
自定义Starter已经可以使用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值