springboot的类加载器(org.springframework.boot.loader)

类加载器的分类。
在这里插入图片描述
试验:使用maven打包

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>启动类的完整路径</mainClass>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

各个配置项的作用自行百度~作用呢就是把lib和jar包分离以及排除一些依赖,target目录如下:

在这里插入图片描述

这样生成的jar包,用压缩工具打开以后是这样
在这里插入图片描述
在META-INF下打开MANIFEST.MF文件看看(记事本就可以打开)

在这里插入图片描述
可以看到这里的类加载器是:PropertiesLauncher,文档百度翻译如下:

Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.
Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn’t exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn’t exist):
loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used
loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.

启动程序,用于通过属性文件使用用户配置的类路径和主类进行归档。与基于可执行jar的模型相比,这种模型通常更灵活,更易于创建性能良好的OS级服务。

在不同位置查找属性文件以提取加载程序设置,默认为应用程序.属性在当前类路径或当前工作目录中。属性文件的名称可以通过设置系统属性来更改加载程序.config.name(例如-Dloader.config.name=foo会寻找食品属性. 如果该文件不存在,则尝试loader.config.location(使用允许的前缀classpath:和file:或任何有效的URL)。找到该文件后,将其转换为属性并提取可选值(如果该文件不存在,也可以将其作为系统属性进行覆盖):

加载程序.path:以逗号分隔的目录列表(包含文件资源和/或.jar或.zip中的嵌套存档文件)或要附加到类路径的存档文件。总是使用应用程序档案中的BOOT-INF/classes、BOOT-INF/lib

装载机.main:设置类装入器后将执行委托给的主方法。没有默认值,但将返回到在清单.MF,如果有${装载机.home}/中导。

这种也是配置:使用jar包外部的配置文件的启动方式的方法。在linux我们的启动脚本要使用对应的类加载器来启动。

在这里插入图片描述
如果你配置成其他的( <layout>ZIP</layout>此配置项去掉,main-class就会变成JarLauncher)比如:JarLauncher

Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.

基于JAR的档案的启动程序。这个启动程序假设依赖项jar包含在/BOOT-INF/lib目录中,应用程序类包含在/BOOT-INF/classes目录中。

就会有如下的错误:项目启动失败。在这里插入图片描述
springboot项目启动,调用的是相应的类加载器的main方法,而不是我们自己编写的SpringApplication

Spring Boot Loader提供了一套标准用于执行SpringBoot打包出来的jar,这套标准就是我们的类加载器

下面是一个例子,图中有构造方法,和类方法。
在这里插入图片描述
友情链接
springboot整个api

PropertiesLauncher文档

JarLauncher文档

WarLauncher文档

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'weChatInfoController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'WeChatInfoService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'weChatInfoMapper' defined in file [E:\java_project\MyWeChat\MyWeChat-module-wechat\MyWeChat-module-wechat-biz\target\classes\com\dabao\mywechat\module\wechat\mapper\WeChatInfoMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.RuntimeException: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader
最新发布
04-03
<think>好的,我现在需要帮助用户解决Spring Boot项目中由于缺少MySQL驱动导致的Bean创建失败问题。用户提到了BeanCreationException,Hikari数据源和MySQL驱动缺失的问题。首先,我应该回忆一下常见的Spring Boot配置问题,特别是与数据库连接相关的错误。 首先,用户提到的错误信息中有提到HikariDataSource实例化失败,可能是因为找不到合适的驱动。根据引用[2],用户可能在使用不同版本的MySQL连接器时遇到了问题。例如,Spring Boot 2.1.0默认使用8.x版本的MySQL驱动,而如果连接的MySQL服务器版本较低,可能需要手动指定5.x版本的驱动。这时候,用户需要在构建文件中明确指定驱动版本,比如在Gradle中使用runtimeOnly('mysql:mysql-connector-java:5.1.46')。同时,需要注意排除自动配置可能带来的冲突,比如是否需要排除Hibernate的自动配置。 接下来,用户可能没有正确配置数据源。引用[3]提到数据源名称未设置,引用[4]和[5]也涉及到多数据源配置的问题。如果用户使用了多数据源,需要确保每个数据源的配置正确,包括URL、用户名、密码,以及驱动类名是否正确。例如,驱动类名对于5.x版本是com.mysql.jdbc.Driver,而8.x版本是com.mysql.cj.jdbc.Driver。 另外,检查依赖管理是否正确。如果项目中使用的是Spring Boot的父POM,可能已经管理了依赖版本,但如果用户手动指定了版本,需要确保没有冲突。比如,在Maven中,可能需要覆盖mysql-connector-java的版本。 还有可能的是,用户的配置文件(application.properties或application.yml)中的数据库连接参数有误,例如拼写错误或者格式不正确。例如,URL中的时区设置是否完整,如serverTimezone=UTC。 此外,用户可能在启动类上没有正确排除某些自动配置,尤其是当使用多数据源时,可能需要排除DataSourceAutoConfiguration,让自定义配置生效。引用[5]提到,如果不需要数据库操作,可以在启动类上排除相关配置,但用户显然需要数据库连接,所以正确的做法应该是确保自动配置不会干扰自定义的多数据源设置。 最后,建议用户检查IDE中的依赖是否实际下载成功,有时候依赖可能未正确下载,需要清理并重新构建项目。另外,查看完整的堆栈跟踪,确定错误发生的具体位置,比如是否在初始化数据源时无法找到驱动类,这通常意味着驱动未正确添加到类路径中。 总结可能的解决方案步骤:1. 检查并添加正确的MySQL驱动依赖;2. 验证数据源配置是否正确;3. 确保依赖版本与MySQL服务器兼容;4. 处理多数据源配置时的排除项;5. 检查配置文件中的连接参数;6. 清理和重新构建项目以确保依赖正确加载。</think>### Spring Boot项目解决DataSource创建失败问题分步指南 #### 1. 添加正确的MySQL驱动依赖 根据引用[2],Spring Boot 2.1.0默认使用MySQL 8.x驱动,若需使用低版本需显式指定: ```gradle // Gradle配置示例 dependencies { runtimeOnly 'mysql:mysql-connector-java:5.1.46' // 指定5.x驱动版本 } ``` ```xml <!-- Maven配置示例 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> ``` #### 2. 验证数据源配置 检查`application.yml`配置文件: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/dbname?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver # 5.x版本驱动类名 ``` #### 3. 处理多数据源配置冲突 若使用动态数据源需排除自动配置(引用[4][5]): ```java @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 4. 检查驱动类加载状态 在启动日志中搜索以下关键词: ``` Loading class `com.mysql.jdbc.Driver'. This is deprecated. ``` 若出现该提示需改用8.x驱动: ```yaml driver-class-name: com.mysql.cj.jdbc.Driver # 8.x驱动类名 ``` #### 5. 依赖树验证 执行命令检查依赖冲突: ```bash # Maven项目 mvn dependency:tree # Gradle项目 gradle dependencies ``` #### 6. 完整错误排查流程 [^1]BeanInstantiationException -> [^2]检查驱动版本 -> [^3]验证数据源配置 -> [^4]多数据源特殊处理 -> [^5]排除自动配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值