• 自己写spring boot starter问题总结


    1. Unable to find main class

    创建spring boot项目写自己的starterxi写完之后使用install出现Unable to find main class,这是因为spring boot打包需要一个启动类,按照以下写法就没事

    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                <layout>NONElayout>
                <skip>trueskip>           
            configuration>
        plugin>
    plugins>          
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    NONE解决启动类问题,true 解决打出jar包不包含BOOT-INF目录 ,否则依赖stater总是报某某包未找到

    非正常

    在这里插入图片描述

    正常

    在这里插入图片描述

    2. 版本问题

    在引入自己的starter之后,一直无法自动注入写的Bean并且代码没问题。此时需要注意你的spring boot版本。3.0.x版本之后自动装配的文件命名不再是spring.factories

    而是org.springframework.boot.autoconfigure.AutoConfiguration.imports文件命名必须要按照这个名字。具体原因可以参考spring boot自动转配原理,如下

    AutoConfigurationImportSelector类主要就是扫描自动装配文件并注册相关的类,这个类的getCandidateConfigurations方法: configurations这个集合里面就是所有应该自动装配的类的路径,根据提示可知自动装配的文件应该写在META-INF/spring/

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    		List<String> configurations = ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader())
    			.getCandidates();
    		Assert.notEmpty(configurations,
    				"No auto configuration classes found in "
    						+ "META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "
    						+ "are using a custom packaging, make sure that file is correct.");
    		return configurations;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    还有一点需要注意就是starter的版本必须要跟需要使用的工程项目spring boot一致否则报错

  • 相关阅读:
    Centos误删系统自带python2.7,yum报错恢复方法
    卷积神经网络提取的图像特征包括哪些
    Docker镜像仓库
    【随笔】Git 高级篇 -- 分离 HEAD(十一)
    linux批量解压zip
    走进Redis-常用指令
    C++左右值及引用
    前端技术点滴整理-1
    Hadoop学习笔记:运行wordcount对文件字符串进行统计案例
    企业信息化安全方案设计实战参考
  • 原文地址:https://blog.csdn.net/m0_64332518/article/details/133864040