天天看點

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

作者:小誇大誇

為了擴充spring boot starter的能力的時候,我們通常要建立一個工具類的starter,然後其它spring boot項目引用這個工具starter。下面是spring boot 的規範:

SpringBoot 定義了一套接口規範,這套規範規定:SpringBoot 在啟動時會掃描外部引用 jar 包中的META-INF/spring.factories檔案,将檔案中配置的類型資訊加載到 Spring 容器(此處涉及到 JVM 類加載機制與 Spring 的容器知識),并執行類中定義的各種操作。對于外部 jar 來說,隻需要按照 SpringBoot 定義的标準,就能将自己的功能裝置進 SpringBoot。

一、建立starter工具類項目

1、建立項目

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

2、用spring 初始化項目來建立

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

3、建立完後檢視項目的關鍵資訊

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

4、依賴類庫

這個預設是會有的

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>           
手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

注意下面的要注釋掉,否則在别一個項目引用這個工具包的時候會報:

UNABLE TO READ META-DATA FOR CLASS異常

如下注釋掉:

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

5、建立一個線程類再為例子

類名為:ThreadPoolAutoConfiguration

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

具體的代碼如下:

package com.example.demo.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolAutoConfiguration {

    @Bean
    @ConditionalOnClass(ThreadPoolExecutor.class)
    public ThreadPoolExecutor myThreadPoolExecutor(){
        return new ThreadPoolExecutor(16, 32, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
    }
}
           

代碼的意思為:建立一個核心線程數為16,最大線程數為32,keepAliveTime為10秒,隊列容量為100的線程執行器。并配置為配置類,傳回一個bean。

6、在threadpool-spring-boot-starter工程的 resources 包下建立META-INF/spring.factories檔案,如下:

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

内容為:

\為轉行的意思,如果有多個類用“,”分隔。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.demo.config.ThreadPoolAutoConfiguration           

Spring Boot 通過@EnableAutoConfiguration開啟自動裝配,通過 SpringFactoriesLoader 最終加載META-INF/spring.factories中的自動配置類實作自動裝配,自動配置類其實就是通過@Conditional按需加載的配置類,想要其生效必須引入spring-boot-starter-xxx包實作起步依賴

7、編譯代碼

點選maven的install安裝,其它的項目就可以引用了。

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

二、建立項目,并引用starter工具

1、用一樣的過程建立項目

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

2、建立完項目後,引入starter工具包

如下為引入的工具包

<dependency>
            <groupId>com.example</groupId>
            <artifactId>threadpool-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>           
手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

注意,starter不可以有編譯的插件,但是這裡是可以有建構的插件的。

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

3、編寫測試類測試

package com.example.demotest;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.concurrent.ThreadPoolExecutor;

@SpringBootTest
class DemotestApplicationTests {

    @Autowired
    private ThreadPoolExecutor myThreadPoolExecutor;

    @Test
    public void test() {
        System.out.println(myThreadPoolExecutor.getCorePoolSize());
    }

}
           

如下:

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

檢視輸出:

手寫spring boot starter及UNABLE TO READ META-DATA FOR CLASS異常處理

和配置的一緻。