组成

  1. Autoconfigure:负责bean的自动装配;
  2. Starter:负责相关Jar包的集中管理,及依赖Autoconfigure;

You may combine the auto-configuration code and the dependency management in a single module if you don’t need to separate those two concerns.

命名规范

starter

  • 官方:spring-boot-starter-{name};
  • 自定义:{name}-spring-boot-starter;

autoconfigure

  • 官方:spring-boot-autoconfigure-{name};
  • 自定义:{name}-spring-boot-autoconfigure;

关键点

1、spring.factories

  • 位置:META-INF/spring.factories
    自定义Starter
  • 内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration

2、自动装配类

  • @Conditional***:使用条件注解,只有满足特定条件才装配bean;
  • @AutoConfigureAfter或@AutoConfigureBefore:设定bean的装配顺序;
@Configuration
@EnableConfigurationProperties(LrpProperties.class)
public class LrpAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public LrpService lrpService(){
        return new LrpService();
    }

}

示例代码

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>

  <groupId>com.didi.springboot</groupId>
  <artifactId>lrp-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>lrp-spring-boot-starter</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

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

</project>

自动装配类

@Configuration
@EnableConfigurationProperties(LrpProperties.class)
public class LrpAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public LrpService lrpService(){
        return new LrpService();
    }

}

配置属性类

@ConfigurationProperties(prefix = LrpProperties.LRP_PREFIX)
public class LrpProperties {
    public static final String LRP_PREFIX = "lrp";

    private String username;
    private String remoteAddress;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getRemoteAddress() {
        return remoteAddress;
    }

    public void setRemoteAddress(String remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "LrpProperties{" +
                "username='" + username + '\'' +
                ", remoteAddress='" + remoteAddress + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

服务类

public class LrpService {

    @Autowired
    private LrpProperties properties;

    public String getData(){
        return properties.toString();
    }
}

spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.didi.springboot.LrpAutoConfiguration

参考:

  1. 官方教程:https://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-custom-starter-naming;

相关文章: