Springboot4项目构建原生镜像的问题解决(持续更新,请收藏)

发布时间:2026/10/29 17:01:08
Springboot4项目构建原生镜像的问题解决(持续更新,请收藏) 一个项目升级到springboot4构建成原生镜像后各种问题经过一番摸索终于解决了。1、ehcache配置问题异常信息Caused by: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/workspace/config-common/ehcache3.xml at org.ehcache.xml.XmlConfiguration.init(XmlConfiguration.java:127) at org.ehcache.xml.XmlConfiguration.init(XmlConfiguration.java:93) at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:346) ... 160 more Caused by: javax.xml.bind.JAXBException - with linked exception: [java.lang.NoSuchMethodException: com.sun.xml.bind.v2.ContextFactory.createContext([Ljava.lang.Class;, java.util.Map)] at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:305) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:286) at javax.xml.bind.ContextFinder.find(ContextFinder.java:409) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662) at org.ehcache.xml.ConfigurationParser.init(ConfigurationParser.java:118) at org.ehcache.xml.XmlConfiguration.init(XmlConfiguration.java:117) ... 162 more解决方法JAXB 实现版本不匹配需要确保引入的 JAXB 实现与 Ehcache 3 兼容并且支持 Native。但我正好想更换成redis缓存所以直接更换了。2、问题如下Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user ${spring.datasource.username} at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:835) at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:307) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:365) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:52) at org.postgresql.jdbc.PgConnection.init(PgConnection.java:290)我是在一个配置文件里配置了spring.datasource.username另一个配置文件引用这个变量构建原生镜像后它解析不了这个值。解决方法是设置两个环境变量DB_USER, DB_PASSWORD这样在K8S里还可以加密比较好。3、各类ClassNotFound异常如Caused by: java.lang.IllegalStateException: Cannot load driver class: com.xcmg.finance.tmp.common.datasource.DynamicDataSource at org.springframework.util.Assert.state(Assert.java:102) at org.springframework.boot.jdbc.autoconfigure.DataSourceProperties.findDriverClassName(DataSourceProperties.java:187) at org.springframework.boot.jdbc.autoconfigure.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:177) at org.springframework.boot.jdbc.autoconfigure.PropertiesJdbcConnectionDetails.getDriverClassName(PropertiesJdbcConnectionDetails.java:51) at org.springframework.boot.jdbc.autoconfigure.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:63)这类异常很是头疼摸索了很久终于知道原因Springboot4构建 原生镜像时会先启动应用根据启动应用用到了哪些构件它会把相应的构件包含进去用不到的会被丢弃所以我们要设置一个正确的profiles.active让它把正式启动时用到的所有类都加载进去。解决方法设置一个环境变量再构建exportSPRING_PROFILES_ACTIVEcommon,logging,local,logging mvn spring-boot:build-image-Dmaven.test.skiptrue-Pxcmg,native4、方法不允许反射的问题Caused by: org.graalvm.nativeimage.MissingReflectionRegistrationError: Cannot reflectively invoke method public void com.zaxxer.hikari.HikariConfig.setDriverClassName(java.lang.String). To allow this operation, add the following to the reflection section of reachability-metadata.json and rebuild the native image: { type: com.zaxxer.hikari.HikariConfig, methods: [ { name: setDriverClassName, parameterTypes: [ java.lang.String ] } ] } The reachability-metadata.json file should be located in META-INF/native-image/group-id/artifact-id/ of your project. For further help, see https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection at org.graalvm.nativeimage.builder/com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils.reportInvokedExecutable(MissingReflectionRegistrationUtils.java:110)解决方法添加一个文件META-INF/native-image///reachability-metadata.json内容如下{ reflection: [ { type: com.zaxxer.hikari.HikariConfig, allDeclaredConstructors: true, allDeclaredMethods: true, allPublicConstructors: true, allPublicMethods: true, fields: [ { name: driverClassName, allowWrite: true }, { name: jdbcUrl, allowWrite: true }, { name: username, allowWrite: true }, { name: password, allowWrite: true }, { name: maximumPoolSize, allowWrite: true }, { name: minimumIdle, allowWrite: true }, { name: idleTimeout, allowWrite: true }, { name: connectionTimeout, allowWrite: true }, { name: validationTimeout, allowWrite: true }, { name: maxLifetime, allowWrite: true }, { name: poolName, allowWrite: true } ] }, { type: com.zaxxer.hikari.HikariDataSource, allDeclaredConstructors: true, allDeclaredMethods: true, allPublicConstructors: true, allPublicMethods: true }, { type: org.postgresql.Driver, allDeclaredConstructors: true, allDeclaredMethods: true }, { type: org.postgresql.jdbc.PgDriver, allDeclaredConstructors: true, allDeclaredMethods: true } ] }我的完整路径src/main/resources/META-INF/native-image/com.xcmg.finance.tmp/tmp-base-service/tmp-base-service/reachability-metadata.json5、类在heap中找不到日志如下[INFO] [creator] Fatal error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: An object of type org.apache.logging.slf4j.SLF4JServiceProvider was found in the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are initialized at run time by default. [INFO] [creator] This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time. [INFO] [creator] [INFO] [creator] You now have two options to resolve this: [INFO] [creator] [INFO] [creator] 1) If it is intended that objects of type org.apache.logging.slf4j.SLF4JServiceProvider are persisted in the image heap, add [INFO] [creator] [INFO] [creator] --initialize-at-build-timeorg.apache.logging.slf4j.SLF4JServiceProvider [INFO] [creator] [INFO] [creator] to the native-image arguments. Note that initializing new types can store additional objects to the heap. It is advised to check the static fields of org.apache.logging.slf4j.SLF4JServiceProvider to see if they are safe for build-time initialization, and that they do not contain any sensitive data that should not become part of the image. [INFO] [creator] [INFO] [creator] 2) If these objects should not be stored in the image heap, you can use [INFO] [creator] [INFO] [creator] --trace-object-instantiationorg.apache.logging.slf4j.SLF4JServiceProvider [INFO] [creator]解决方法在spring-boot-maven plugin中添加参数把相应的包名加进去。如plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId version${spring-boot.version}/version executions execution goals goalrepackage/goal /goals /execution /executions configuration image !-- 自定义应用镜像名称可选 -- name10.16.10.28:8085/tmp/${project.artifactId}:${project.version}/name env BP_DEPENDENCY_MIRRORhttps://irmp.xcmglease.com:9443/download/BP_DEPENDENCY_MIRROR BP_NATIVE_IMAGE_BUILD_ARGUMENTS--initialize-at-build-timeorg.springframework.boot.logging.log4j,org.slf4j,org.apache.logging.slf4j,org.apache.logging.log4j,io.netty.util.internal.logging --initialize-at-run-timeio.lettuce.core.resource.KqueueProvider,io.netty.util.internal.SystemPropertyUtil,com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration/BP_NATIVE_IMAGE_BUILD_ARGUMENTS JAVA_TOOL_OPTIONS-Dfile.encodingUTF-8 -Duser.timezoneAsia/Shanghai -XX:MaxRAMPercentage75.0 -XX:InitialRAMPercentage50.0 -XX:MaxMetaspaceSize512m -XX:UseStringDeduplication -XX:ExitOnOutOfMemoryError -Dspring.aot.enabledtrue -XX:PrintCommandLineFlags/JAVA_TOOL_OPTIONS /env /image /configuration /plugin本类错误会很多出现一个加一个包名进去再试就好了。

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询