定義一個SpringBoot Starter

Starter是SpringBoot中的一個非常重要的概念,Starter相當(dāng)于模塊,它能將模塊所需的依賴整合起來并對模塊內(nèi)的Bean根據(jù)條件進行自動配置。使用者只需要依賴相應(yīng)功能的Starter,無需做過多的配置和依賴,SpringBoot就能自動掃描并加載相應(yīng)的模塊,例如我們在創(chuàng)建SpringBoot項目時,經(jīng)常會引入如spring-boot-starter-web這種依賴,該依賴為我們做了很多默認配置,無需再依賴spring-webspring-webmvc等相關(guān)包及做相關(guān)配置就能夠立即使用它。

本文將通過一個簡單的案例介紹如何定義一個Starter

一、編寫Starter

1、導(dǎo)入依賴

這里將SpringBoot自帶的編譯替換成了apache的,不然會提示找不到主類的哈

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2、定義一個注解

該注解會在標注的方法被執(zhí)行時,進行日志輸出

/**
 * @author Gjing
 **/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Print {
    
}

3、編寫AOP處理注解

這里通過AOP對注解進行處理,如果對AOP不熟悉的話,可以參考我的這篇文章:SpringBoot使用AOP

/**
 * @author Gjing
 **/
@Aspect
@Component
public class PrintProcess {

    @Pointcut("@annotation(com.gjing.Print)")
    public void cut() {

    }

    @Around("cut()")
    public Object printCut(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法: " + method.getName() + " 開始執(zhí)行, 當(dāng)前時間: " + LocalDateTime.now());
        Object proceed = joinPoint.proceed();
        System.out.println("方法: " + method.getName() + " 執(zhí)行結(jié)束, 當(dāng)前時間: " + LocalDateTime.now());
        return proceed;
    }
}

4、編寫配置類

/**
 * @author Gjing
 **/
@Configuration
public class PrintConfiguration {

    @Bean
    public PrintProcess printProcess() {
        return new PrintProcess();
    }
}

5、配置自動裝配

在resources文件夾下新建一個META-INF包,并創(chuàng)建一個spring.factories文件,如下

package.png

spring.factories文件內(nèi)容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gjing.PrintConfiguration

其中=號右邊為您的配置類,需要指定包名

二、編寫測試項目

1、導(dǎo)入依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--這個為我們定義的starter-->
<dependency>
    <groupId>com.gjing</groupId>
    <artifactId>my-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2、定義一個接口

定義接口并在方法上使用我們starter里定義的注解

/**
 * @author Gjing
 **/
@RestController
public class TestController {

    @PostMapping("/test")
    @Print
    public String test() {
        return "ok";
    }
}

3、運行測試

啟動項目并進行測試,從控制臺可以看到輸出日志:


console.png

4、總結(jié)

從上面例子中可以看出,我們在使用這個注解的時候,沒有做任何的其他配置便可以去使用它,這正是SpringBoot自動裝配帶來的便利性,在傳統(tǒng)的Spring項目中,我們往往需要手動去配置一些和交給Spring的IOC容器進行管理,這就顯得有點繁瑣和復(fù)雜。


本文到此就結(jié)束,文章中只舉了簡單的例子,更多高層次的用法各位讀者可以自行去摸索,本文不在闡述。如果本文有任何誤點,可以在評論區(qū)留言。本文Demo地址:SpringBoot-Demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容