It is well known (not known? Spring Boot is made up of many starters, and the Starter family grows with each release. In traditional Maven projects, some layers and components are divided into modules to be managed for mutual reuse. In Spring Boot projects, we can create a custom Spring Boot Starter to achieve this purpose.

To start, create a Maven project and import the dependency. Pom.xml is shown below for your reference

<?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.example</groupId>
    <artifactId>example-spring-boot-starter</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <! -- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.2. RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>Copy the code

Spring-boot-starter -{name} spring-boot-starter-web It is recommended that the unofficial Starter be named in the {name}-spring-boot-starter format.

Here’s what our Starter will do. It’s very simple. It provides a Service that contains a String wrap(String word) method that adds a prefix or suffix to a String.

public class ExampleService {

    private String prefix;
    private String suffix;

    public ExampleService(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = suffix;
    }
    public String wrap(String word) {
        return prefix + word+ suffix; }}Copy the code

The prefix and suffix are obtained by reading the parameters in application.properties(yML)

@ConfigurationProperties("example.service")
public class ExampleServiceProperties {
    private String prefix;
    private String suffix;
    // Omit the getter setterCopy the code

Most importantly, write the AutoConfigure class

@Configuration
@ConditionalOnClass(ExampleService.class)
@EnableConfigurationProperties(ExampleServiceProperties.class)
public class ExampleAutoConfigure {

    @Autowired
    private ExampleServiceProperties properties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")
    ExampleService exampleService (){
        return  new ExampleService(properties.getPrefix(),properties.getSuffix()); }}Copy the code

A few notes about Starter:

  • @ConditionalOnClasswhenclasspathIs automatically configured when the class is discovered.
  • @ConditionalOnMissingBeanwhenSpring ContextDoes not exist inBeanAt the right time.
  • @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true"), when the configuration fileexample.service.enabled=trueAt the right time.

For more notes, please read this section of the official documentation.

Finally, create a spring.factories file under Resources/meta-INF /

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigureCopy the code

OK, that’s it. Run MVN: Install to package the installation and a Spring Boot Starter is ready. If you need the Starter source code, click here.


Create a Spring Boot project to try ~

Introduce the example-spring-boot-starter dependency

 <dependency>
    <groupId>com.example</groupId>
    <artifactId>example-spring-boot-starter</artifactId>
    <version>1.0 the SNAPSHOT</version>
 </dependency>Copy the code

Create application.properties and configure it

example.service.enabled=true
example.service.prefix=####
example.service.suffix=@@@@Copy the code

Create a simple Spring Web Application and inject the ExampleService provided by the Starter to see if it works

@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private ExampleService exampleService;

    @GetMapping("/input")
    public String input(String word){
        return exampleService.wrap(word); }}Copy the code

Start Application and try accessing the /input interface






Summarize how Starter works

  1. Spring BootThe JAR packages that the project depends on are scanned at startup for containsspring.factoriesJAR package for the file
  2. According to thespring.factoriesConfiguration is loadedAutoConfigureclass
  3. According to the@ConditionalAnnotated conditions that are automatically configured and willBeaninjectionSpring Context