[SpringBoot] Encapsulate your own starter from scratch and introduce it into other projects for use

Encapsulate your own starter from scratch and introduce it into other projects for use

Introduction

This article will introduce how to package your own starter from scratch and introduce it into other projects for use

Why do you need to package the starter yourself?
In this way, starters provided by spring and other third parties can be re-encapsulated or some content you need can be packaged for other projects to improve project-level code reusability.

1. Create a new spring-boot project

First of all, we need to create a new spring-boot project. In order to prevent maven dependency problems, we do not choose any spring-provided components here. If you add it, please make sure that the referenced project does not have this component . Also don't forget to change the maven source

insert image description here

2. Create META-INF/spring.factories

Delete the test folder and create the resources/META-INF/spring.factories file in the main directory
insert image description here

Delete the main program too, our starter does not need to start

insert image description here

3. Test case

Next, write an interceptor, intercept all methods and print some content to see the effect

First delete the messy things in the pom file and introduce a web module

	<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Config file

package com.ez4sterben.mytestspringbootstarter.config;

import com.ez4sterben.mytestspringbootstarter.interceptor.MyTestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 我的测试web mvc配置
 *
 * @author ez4sterben
 * @date 2023/07/18
 */
@Configuration
public class MyTestWebMvcConfig implements WebMvcConfigurer {
    
    

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    
    
        registry.addInterceptor(new MyTestInterceptor())
                .addPathPatterns("/**");
    }
}

Interceptor file

package com.ez4sterben.mytestspringbootstarter.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 我的测试拦截器
 *
 * @author ez4sterben
 * @date 2023/07/18
 */
@Component
public class MyTestInterceptor implements HandlerInterceptor {
    
    

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        System.out.println("--------------------starter中的拦截器生效了!------------------------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    

    }
}

Add the following content to the spring.factories file to enable automatic configuration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.ez4sterben.mytestspringbootstarter.config.MyTestWebMvcConfig

The current project structure is as follows

insert image description here

4. Package starter

Open maven on the right side of the idea and click install and package in the life cycle
insert image description here

5. Use the starter

Open our local maven repository

insert image description here

Our starter is already in the form of a jar package, and then we will open our other projects to reference this starter

        <dependency>
            <groupId>com.ez4sterben</groupId>
            <artifactId>mytest-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

Then refresh the maven startup project, just find an interface to test it
insert image description here

insert image description here
Test success!

6. Summary

This article makes a relatively basic case. You can try to encapsulate your own starter in this way, and the web module has been introduced in the starter, so there is no need to introduce it in the project that introduces the starter. In this way, the basic content that is fixed in many projects can be encapsulated, such as general return values, basic constants, basic enumeration classes, and some unified authentication and unified exception interception . After these contents are configured once, they can be used in various projects.

Guess you like

Origin blog.csdn.net/qq_51383106/article/details/131787861