Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to create a SpringBoot custom Starter

Shulou Source: shulou.com Published: 2022-06-01 11:01:28 11月06日 Update

This article mainly explains "how to create a SpringBoot custom Starter", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to create a SpringBoot custom Starter" bar!

There's nothing magical about SpringBoot's Starter. It's just that a lot of people have asked about me recently, and my Baidu search index tells me that the number of people searching for druid-spring-boot-starter is increasing recently. So, I will give you popular science today, how to customize the implementation of a SpringBoot Starter.

Any learning starts with imitation, so let's take a look at the structure of mybatis-spring-boot-starter. And then compare and imitate one.

First, we open the pom.xml file of mybatis-spring-boot-starter and you will find that there is a mybatis-spring-boot-autoconfigure among the many dependency referenced by it. Others like JDBC,Mybatis,Spring are necessary to integrate Mybatis in non-starter mode and can be ignored.

Then we follow mybatis-spring-boot-autoconfigure to look at its pom.xml file.

You will find that there are two important references spring-boot-autoconfigure and spring-boot-configuration-processor.

Why care about these two? Because only these two are provided by Spring Boot, and the others are not.

If you haven't found a pattern yet, you can take a look at the structure of spring-boot-starter-jdbc. After watching a lot of starter, you will find that spring-boot-configuration-processor and spring-boot-autoconfigure are introduced into all starter.

In addition, as I said in my previous article, the @ Conditional annotation initiates the role of a connecting link in SpringBoot. According to @ Conditional, @ ConditionalOnClass, @ ConditionalOnMissingBean, @ ConditionalOnProperty, etc., together constitute some choices, on-demand configuration.

In SpringBoot's brain map and source code interpretation, I also said that @ SpringBootApplication will get the full class name of the class that needs to be automatically configured in the META-INF/spring.factories in each starter. The automatic configuration class is created by putting the full name of the automatic configuration class into the ImportSelector, and the corresponding automatic configuration is carried out according to the logic in the automatic configuration class.

So, based on the simple interpretation above, let's get started with a custom Starter.

First, create a Maven project for xttblog-spring-boot-starter. The configuration in pom.xml is as follows:

The function of spring-boot-configuration-processor is to generate spring-configuration-metadata.json at compile time, and this file is mainly used by IDE to prompt for use. For example, in intellij idea, when configuring this jar-related configuration property in application.yml, you can use the left mouse button of ctlr+, and IDE will jump to the class where you configure this property. There is no error without spring-boot-configuration-processor, but there is no hint and it is not perfect, so let's add it.

In addition, look at the naming of my Maven project, xttblog-spring-boot-starter meets the requirements of SpringBoot: {name}-spring-boot-starter. Only the official starter, with the name at the end. Spring-boot-starter- {name}, you can take a look at the above examples of mybatis and druid, both of which conform to the naming convention.

Next, our custom starter implements a simple business. Enter a string and we output: "amateur say,hello string www.xttblog.com".

Then define the prefix and suffix property classes.

Next comes the most important automatic configuration class.

Again, these key @ Conditional.

@ ConditionalOnClass, which is automatically configured when this class is found under classpath.

@ ConditionalOnMissingBean, when the Bean does not exist in Spring Context.

@ ConditionalOnProperty is executed when xttblog.hello.service=true is in the configuration file.

Finally, don't forget spring.factories. Create a spring.factories file under resources/META-INF/, as follows:

Finally, you can run mvn:install to package and install, and a custom Spring Boot Starter is developed. Then you can use xttblog-spring-boot-starter in other SpringBoot projects in this way.

I will not post the specific testing process. If you do it yourself, you will gain more!

Thank you for your reading, the above is the content of "how to create a SpringBoot custom Starter", after the study of this article, I believe you have a deeper understanding of how to create a SpringBoot custom Starter, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Configuration file learning two content properties project important next function only characters strings that is situation articles tips search magic connecting link Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Shulou Tech Info OPPO Reno NVidia