SpringBoot (10) SpringBoot custom starter

    In a month's time, I have reached the tenth article of my SpringBoot series. I still remember my second article SpringBoot (2) starter introduction_springboot's starter_heart tormented blog-CSDN blog  once introduced the starter. In addition to the official starter, we can also customize it. In this article, I will introduce how to customize a tool starter.

    If you are a novice and have not read my previous series of SpringBoot articles, please pay attention to my SpringBoot column, and I will continue to update:

https://blog.csdn.net/qq_21154101/category_12359403.html

Table of contents

1. Import dependencies

2. Create a new implementation class

3. Create a new Config class

4. mvn install

5. Use the starter


1. Import dependencies

    To create a new SpringBoot project, first add the following dependencies to the pom.xml file:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<version>2.7.13</version>
		</dependency>

    Here, there is a small episode. It may be that a new version of SpringBoot has been released recently, so I cannot choose 2.7.13 for my new project. The minimum version can only choose 2.7.14. But after the creation is completed, an error is reported, and aliyun cannot find the corresponding version of SpringBoot, and the springboot related classes cannot be found. So I manually changed to 2.7.13.

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.13</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

2. Create a new implementation class

    In actual projects, we often do some basic operations on strings, so I simply write a text tool class (actually copied from Android's TextUtils, with a slight modification), the code is as follows:

package com.tudu.utils.text;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.lang.Nullable;

@EnableConfigurationProperties(TextUtils.class)
@ConfigurationProperties(prefix = "textUtils")
public class TextUtils {

    public static boolean isEmpty(@Nullable CharSequence str) {
        return str == null || str.length() == 0;
    }

    public static int length(@Nullable String str) {
        return str != null ? str.length() : 0;
    }

    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) {
            return true;
        }
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

}

    The above code provides three methods. In particular, it is necessary to add the following two annotations to the class:

@EnableConfigurationProperties(TextUtils.class)
@ConfigurationProperties(prefix = "textUtils")

3. Create a new Config class

    Next, create a new Config class, especially note that the two added annotations and the @Bean annotation of the method:

package com.tudu.utils;

import com.tudu.utils.text.TextUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass
public class UtilsAutoConfiguration {
    static {
        System.out.println("MyAutoConfiguration init......");
    }

    @Bean
    public TextUtils textUtils() {
        return new TextUtils();
    }
}

4. mvn install

    After the above steps, we have realized the function and configuration of our starter. Next, publish the starter to the local maven, and execute the following command on the command line:

mvn clean install -Dmaven.test.skip=true

  The following errors may be encountered:

 The build tag in the pmo.xml file needs to be deleted:

After deleting, re-execute the mvn command, and the execution is successful:

5. Use the starter

    Next, we will use the customized starter in other projects. Add the following dependencies and sync to other SpringBoot projects:

		<dependency>
			<groupId>com.tudu</groupId>
			<artifactId>utils</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

    In the class in the project, use the method of the TextUtils class defined in the starter. You can see that the class in the starter we just customized is used, as shown below:

    Finally, a brief summary. SpringBoot provides a lot of starters, many of which are official and can also be customized. In this article, I have customized a tool class starter. Currently, there is only one TextUtils, and many tool classes will be added on this basis in the future.

Guess you like

Origin blog.csdn.net/qq_21154101/article/details/132037412