天天看点

手写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异常处理

和配置的一致。