avatarlance

Summary

The web content provides a guide on how to create a custom Spring Boot Starter, leveraging Spring Boot's auto-configuration feature to simplify the configuration process for enterprise Java applications.

Abstract

The article titled "How to Build a Custom Spring Boot Starter" explains the process of creating a personalized starter library in Spring Boot. It emphasizes the convenience of Spring Boot's auto-configuration feature for Java programmers, allowing for the quick setup of applications without the need for complex configurations. The author illustrates the principle behind Spring Boot's auto-configuration, which involves the @EnableAutoConfiguration annotation and the spring.factories file. The article walks through the steps of building a custom starter, including setting up a Spring project, creating an auto-configuration class, and configuring the spring.factories file. The author also provides examples of code and configuration files, demonstrating how to inject a UserManager bean. The article concludes with instructions on how to use the newly created starter in a Maven pom.xml file and showcases the output of the application.

Opinions

  • The author expresses a strong preference for Spring Boot, stating it's indispensable for enterprise Java programming due to its auto-configuration capabilities.
  • Spring Boot's extensibility is highlighted as a key feature, allowing developers to package their own starter libraries.
  • The article suggests that using Spring Boot starters can significantly speed up and simplify the development process.
  • The author seems to assume a certain level of familiarity with Spring Boot, as the explanation of auto-configuration and the creation of a custom starter presuppose some prior knowledge.
  • The inclusion of links to additional resources and tutorials indicates the author's endorsement of continuous learning and preparation for Spring Boot interviews.
  • A recommendation for an AI service is made at the end, suggesting the author values cost-effective tools that offer high performance, akin to ChatGPT Plus (GPT-4).

How to Build a Custom Spring Boot Starter

My code library can be configured automatically using spring boot. It’s so cool.

As an enterprise java programmer, spring boot is so convenient that I can’t live without it. Its auto-configuration feature allows us to run your application in a few simple steps. Spring boot provides many starters to make your development faster and simpler. You don’t have to worry about complex configurations and class library references. Take developing a web service as an example.

  • Using some starters dependencies such as spring-boot-starter and spring-boot-starter-web
  • Add Webserver related parameters, such as server.port
  • Start Server

Can we use spring boot to package our own starter library? The answer is yes. That is spring-boot, a framework with excellent extensibility.

Understanding Auto Configuration

Now let’s try to understand the principle of spring boot's auto-configuration function. To understand this part, you need to have a specific understanding of the life cycle of spring boot beans.

Spring boot uses the @EnableAutoConfigurationannotation to implement the auto-configuration. This annotation import the autoconfigurationImportSelectorclass, which resolves META-INF/spring.factories file when spring boot starts, and then all configuration classes in the file are loaded into memory and registered in the spring container. The spring.factories file is like this.

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration

After the BeanFactory is created, spring boot will call a specific beanFactoryPostProcessor--ConfigurationClassPostProcessorto complete the configuration classes resolution. The purpose is to register the methods modified by the @Bean annotation in the configuration class in the spring container as special beans. The beans defined in the configuration class are managed by the spring container, and we can do anything with spring. This is the auto configuration principle of spring boot.

Create a Custom Starter Library

First, you need to create a spring project. Here is the pom.xml of my example.

Then create your auto configuration class, and add the @configuration annotation on the class like this.

In this demo, I created a MyAutoConfiguration class, which hopes to inject a java bean of UserManager.

Finally, create a directory called META-INF under the resources directory, and create a file called spring.factories, which needs to configure your configuration class. As shown in the following figure:

Congratulations. Your spring boot starter is ready to work. You can use this starter in maven pom.xml like this.

<dependency>
        <groupId>com.lance.demo</groupId>
        <artifactId>my-spring-boot-starter</artifactId>
        <version>1.0.0-SNAPSHOT</version>
   </dependency>

Here is the output of running this application.

Now try to package your class library with spring boot. Thanks for reading.

Spring Boot
Spring Boot Starter
Java
Programming
Coding
Recommended from ReadMedium