springbootstarter

官方命名规范:spring-boot-starter-*
第三方starter命名规范应该遵循:thirdpartyproject-spring-boot-starter
web示例:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

spring-boot-starter-web依赖内容:

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.1.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.1.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.17.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.8.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.8.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

自研starter步骤
1.建工程
2.引入spring-boot-start,spring-boot-autoconfigure,第三方jar
3.如需生成配置元信息,加入spring-boot-configuration-processor依赖
4.编写自动配置类
5.配置发现配置文件:META-INF/spring.factories
6.打包发布
pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.1.6.RELEASE</version>
            <optional>true</optional>
        </dependency>

META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.study.girl.spring.boot.configure.GirlAutoConfigure

configure:

@Configuration
@EnableConfigurationProperties(GirlProperties.class)
public class GirlAutoConfigure {

    @Bean
    public GirlDemo getGirl(GirlProperties girlProperties){
        GirlDemo girlDemo =new GirlDemo();
        girlDemo.setName(girlProperties.getName());
        girlDemo.setId(girlProperties.getId());
        girlDemo.setFace(girlProperties.getFace());
        return girlDemo;

    }
}

properties


@ConfigurationProperties(prefix = "com.study.girl")
public class GirlProperties {
    String name;
    String id;
    String face;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFace() {
        return face;
    }

    public void setFace(String face) {
        this.face = face;
    }

}

使用

com:
  study:
    girl:
      face: aaa
      id: 1
      name: marry
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容