從零開始封裝自己的starter并且引入到其他項目中使用
簡介
本文將介紹如何從零開始封裝自己的starter并且引入到其他項目中使用
為什么要自己封裝starter?
這樣可以對spring以及其他第三方提供的starter做二次封裝或者封裝一些自己需要的內(nèi)容提供給其他項目使用,提高項目級的代碼復(fù)用性。
一、創(chuàng)建一個新的spring-boot項目
首先我們需要新建一個spring-boot項目,為了防止發(fā)生maven依賴問題,我們這里不選擇任何spring提供的組件,如果添加了請確保引用的項目中沒有這個組件。另外不要忘記改maven源
二、創(chuàng)建META-INF/spring.factories
把test文件夾刪掉,main目錄下創(chuàng)建resources/META-INF/spring.factories文件
把主程序也刪掉,我們的starter不需要啟動
三、測試案例
接下來編寫一個攔截器,攔截所有方法并且打印一些內(nèi)容看看效果
先把pom文件中亂七八糟的東西刪干凈,并引入一個web模塊
<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文件
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文件
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 {
}
}
spring.factories文件中加入下面這段內(nèi)容開啟自動配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ez4sterben.mytestspringbootstarter.config.MyTestWebMvcConfig
目前項目結(jié)構(gòu)如下
四、打包starter
打開idea右側(cè)maven依次點擊生命周期中的install以及package
五、使用starter
打開我們的本地maven倉庫
我們的starter已經(jīng)是jar包的形式了,接下來就打開自己的其他項目來引用這個starter
<dependency>
<groupId>com.ez4sterben</groupId>
<artifactId>mytest-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
隨后刷新maven啟動項目即可,隨便找個接口測試一下
測試成功!文章來源:http://www.zghlxwxcb.cn/news/detail-596332.html
六、總結(jié)
本文做了一個比較基礎(chǔ)的案例,大家可以嘗試這種方式封裝自己的starter,而且在starter中已經(jīng)引入了web模塊,所以在引入starter的項目中就不用再引入了,通過這種方式可以把在很多項目都固定寫的基本內(nèi)容封裝起來,比如通用返回值、基本常量、基本枚舉類以及一些統(tǒng)一鑒權(quán)和統(tǒng)一異常攔截。可以讓這些內(nèi)容一次配置好后在各個項目中均可以使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-596332.html
到了這里,關(guān)于【SpringBoot】從零開始封裝自己的starter并且引入到其他項目中使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!