ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Boot自定义Starter编写Demo

2021-05-17 13:32:32  阅读:153  来源: 互联网

标签:自定义 Spring boot public Boot spring org example starter


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已经可以使用了。

标签:自定义,Spring,boot,public,Boot,spring,org,example,starter
来源: https://blog.csdn.net/sprogFall/article/details/116928699

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有