SoFunction
Updated on 2025-05-08

Springboot starter introduction and custom starter examples

Introduction to springboot starter

What is Starter

Spring Boot's Starter is a relatively convenient set of dependency descriptors that can be typed into jar packages through Maven and referenced directly in your project.

With Starter you can get all the dependencies of this feature and the unified configuration, avoiding the hassle of copying, pasting code and dependencies.

Starter mainly uses automatic configuration, so all components below it will be injected into the IOC container by Spring Boot through META-INF/file.

What is the SpringBoot starter mechanism

The starter in SpringBoot is a very important mechanism (automated configuration). It can abandon the complicated configurations of the past and integrate them into the starter. Applications only need to introduce the starter dependency in maven. SpringBoot can automatically scan the information to be loaded and start the corresponding default configuration. starter allows us to get rid of the problem of handling various dependencies and configuring various information. SpringBoot will automatically discover the required beans through the classpath path and register them into the IOC container. SpringBoot provides spring-boot-starter dependency module for various scenarios for daily enterprise applications. All these dependency modules follow the default configurations that are customary to conventions and allow us to adjust these configurations, that is, to follow the concept of "conventions are greater than configuration".

Starter starter naming

#official
spring-boot-starter-jdbc
spring-boot-starter-web
spring-boot-starter-freemarker
#Third Party
sms-spring-boot-starter
myLog-spring-boot-starter

Naming specifications for configuration items

If a custom Starter contains configuration items, use a unique namespace for it and do not conflict with Spring Boot or other components, for example:serverspringwait

Why customize the starter

In our daily development work, there are often some configuration modules that are independent of the business, and we often put them in a specific package.
Then if another project needs to reuse this function, it is extremely troublesome to hard copy the code to another project and reintegrate it.
If we encapsulate these functional configuration modules that can be independent of business code into starters, when reusing, we only need to reference the dependencies in the pom.
SpringBoot completes automatic assembly for us, it's so cool

When do you need to create a custom starter?

In our daily development work, it may be necessary to develop a general module for reuse of other projects. SpringBoot provides us with such a functional mechanism.
We can encapsulate our general modules into starters, so that when other projects are reused, you only need to reference the dependencies in the pom.
Automatic assembly is done for us by SpringBoot.

Common scenarios:
1. General module-SMS sending module
2. Implement log sectioning based on AOP technology
3. Database connection pool configuration for microservice projects
4. Each module of a microservice project must access the redis database, and each module must configure redisTemplate.
It can also be solved by starter

Dependencies required for custom Starter

Introduce two dependencies in POM:

  • spring-boot-starter: This dependency is the core of Starter, including automatic configuration, logging and YAML support. All our custom Starters should introduce this dependency directly or indirectly.
  • spring-boot-configuration-processor: Contains a Java annotation processor. This dependency needs to be introduced when configuring its own metadata using @ConfigurationProperties annotation. Its function is to produce configuration metadata, which can give prompts in the configuration file, such as
<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

in<optional>true</optional>

Added to the dependencies of the parent module<optional>true</optional>, In this case, the dependency jar package will be downloaded directly in the submodule, for<optional>true</optional>To say, whether to add the parent module<dependencyManagement>All are the same, they all need to display the call to reference the dependency jar package

Related comments

@Configuration //Specify that this class is a configuration class@ConditionalOnXXX //Automatic configuration class takes effect if the specified conditions are true@AutoConfigureOrder //Specify the order of automatic configuration classes@AutoConfigureBefore //Before a specific automatic assembly of Class@AutoConfigureAfter //After a specific automatic assembly of Class@Bean //Add components to the container@ConfigurationPropertie //Bind the related xxxProperties class to bind related configuration@EnableConfigurationProperties //Let xxxProperties take effect and add it to the containerAutomatic configuration classes must be able to load Automatic configuration class that will load when it needs to be started,Configure inMETA‐INF/
=\
,\
,\
@Conditional:Make judgments according to certain conditions,Register the container with the conditionsbean
@ConditionalOnMissingBean:Given inbeanWhen it does not exist,Instantiate the currentBean
@ConditionalOnProperty:The attributes that satisfy the definition in the configuration file are createdbean,Otherwise, no creation
@ConditionalOnBean:Given inbeanWhen it exists,Instantiate the currentBean
@ConditionalOnClass: When the given class name exists on the classpath,Instantiate the currentBean
@ConditionalOnMissingClass :When the given class name does not exist on the classpath,Instantiate the currentBean

Custom Starter Example

1. Create a custom Starter project

Create a Spring Boot project and name itdemo-spring-boot-starter

2. Introduce dependencies

<parent>
    <groupId></groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.14</version>
</parent>
<dependencies>
     <dependency>
         <groupId></groupId>
         <artifactId>spring-boot-starter</artifactId>
         <optional>true</optional>
    </dependency>
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId></groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

3. Define a configuration metadata class

Customize a configuration metadata classDemoProperties, used to map configurations in YAML, use@ConfigurationPropertiesThe annotation needs to specify the prefix of the configuration item, and the prefix here ismysyu. You can use it when prompted in the yml file:

The package name can be named

/**
  * @ConfigurationProperties: Bind all properties in this class and related configurations in configuration files.
  */
@Component
@ConfigurationProperties(prefix = "mystu")
@Getter
@Setter
public class DemoProperties {
    /**
      * Properties
      */
    private String name;
    private String sex;
    private String age;
}

4. Create a specific implementation class for this project

/**
  * Here is what to do after obtaining the configuration of this project
  * This example is just a simple demonstration of the basic use of starter, just output
  * For example, the configuration is to send text messages, then after obtaining the mobile phone number parameters here, the sending function code can be realized.
  */
public class DemoService {
    private DemoProperties demoProperties;
    public DemoService(DemoProperties demoProperties){
         = demoProperties;
    }
    public void print(){
        ("Acquired name:"+());
        ("Gender obtained:"+());
        ("Age obtained:"+());
    }
}

5. Create a configuration class

DemoAutoConfiguration, can be placed in the same package as the metadata class

// This class is a configuration class@Configuration(proxyBeanMethods = false)
// @ConditionalOnClass is a conditional annotation in Spring Boot. It is used to specify that the annotated bean is created and initialized when a certain class exists.// Put it here and there is a DemoService component in the container. DemoAutoConfigure will be created@ConditionalOnClass(value = {})
// Import our customized configuration class for use in the current class@EnableConfigurationProperties(value = )
public class DemoAutoConfigure {
    @Bean
    // This object does not exist in the container, a Bean will be instantiated    @ConditionalOnMissingBean()
    public DemoService helloService(DemoProperties demo) {
        //Inject the obtained information into        DemoService demoService = new DemoService(demo);
        return demoService;
    }
}

@Configuration(proxyBeanMethods = false)

First, two concepts are introduced: Full full mode, Lite lightweight mode

  • Full(proxyBeanMethods = true) : When the proxyBeanMethods parameter is set to true, it is: Full full mode. In this mode, the same component injected into the container is the same bean instance no matter how many times it is taken out, that is, a single instance object. In this mode, SpringBoot will determine whether the component exists in the container every time it is started.
  • Lite(proxyBeanMethods = false) : When the proxyBeanMethods parameter is set to false, it is: Lite lightweight mode. In this mode, the same component injected into the container is a different bean instance no matter how many times it is taken out, that is, a multi-instance object. In this mode, SpringBoot will skip checking whether the component exists in the container every time it is started.
  • When to use Full full mode, when to use Lite lightweight mode? When there is a dependency between bean instances injected into the container in the same Configuration configuration class, it is recommended to use Full full mode. When there is no dependency between bean instances injected into the container in the same Configuration configuration class, it is recommended to use Lite lightweight mode to improve the startup speed and performance of springboot

6. Define the candidate for automatic configuration

existresourcesAdd files to the resource directoryMETA-INF/and to customize the configuration classDemoAutoConfigurationAdd to the automatic configuration class list, as follows:

=\

Under the key configured with the automatic configuration class DemoAutoConfiguration, springboot will automatically load the file and assemble according to the conditions.

Spring Boot will check whether all published jars contain META-INF/files and inject the target class in the file into the IOC container. The automatic configuration class is loaded in this way.

7. Test

After all the above steps are completed, use the maven command to package it

mvn clean install -=true

Or package through IDEA's install

Finally, introduce this project dependency and test it where you need to import the project.

This is the end of this article about springboot starter introduction and custom starter examples. For more related springboot custom starter content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!