最近在学习JDK17的时候遇到这么一个问题,springBoot启动失败,日志如下:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to instantiate factory class [com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer] for factory type [org.springframework.context.ApplicationContextInitializer]at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$throwing$0(SpringFactoriesLoader.java:650)at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$handleMessage$3(SpringFactoriesLoader.java:674)at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:231)at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206)at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160)at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:459)at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:455)at org.springframework.boot.SpringApplication.(SpringApplication.java:273)at org.springframework.boot.SpringApplication.(SpringApplication.java:252)at com.zzz.train.business.BusinessApplication.main(BusinessApplication.java:20)Caused by: java.lang.reflect.InvocationTargetExceptionat java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.instantiate(SpringFactoriesLoader.java:381)at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:228)... 7 moreCaused by: com.ctrip.framework.apollo.exceptions.ApolloConfigException: Unable to load instance for com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory!at com.ctrip.framework.apollo.spring.util.SpringInjector.getInstance(SpringInjector.java:40)at com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer.(ApolloApplicationContextInitializer.java:48)... 14 moreCaused by: com.ctrip.framework.apollo.exceptions.ApolloConfigException: Unable to initialize Apollo Spring Injector!at com.ctrip.framework.apollo.spring.util.SpringInjector.getInjector(SpringInjector.java:24)at com.ctrip.framework.apollo.spring.util.SpringInjector.getInstance(SpringInjector.java:37)... 15 moreCaused by: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerErrorat com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)at com.google.common.cache.LocalCache.get(LocalCache.java:3937)at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4830)at com.google.inject.internal.FailableCache.get(FailableCache.java:48)at com.google.inject.internal.ConstructorInjectorStore.get(ConstructorInjectorStore.java:50)at com.google.inject.internal.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:138)at com.google.inject.internal.InjectorImpl.initializeBinding(InjectorImpl.java:536)at com.google.inject.internal.AbstractBindingProcessor$Processor$1.run(AbstractBindingProcessor.java:160)at com.google.inject.internal.ProcessedBindingData.initializeBindings(ProcessedBindingData.java:44)at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:123)at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)at com.google.inject.Guice.createInjector(Guice.java:99)at com.google.inject.Guice.createInjector(Guice.java:73)at com.google.inject.Guice.createInjector(Guice.java:62)at com.ctrip.framework.apollo.spring.util.SpringInjector.getInjector(SpringInjector.java:22)... 16 moreCaused by: java.lang.ExceptionInInitializerErrorat com.google.inject.internal.cglib.reflect.$FastClass$Generator.getProtectionDomain(FastClass.java:73)at com.google.inject.internal.cglib.core.$AbstractClassGenerator.create(AbstractClassGenerator.java:206)at com.google.inject.internal.cglib.reflect.$FastClass$Generator.create(FastClass.java:65)at com.google.inject.internal.BytecodeGen.newFastClassForMember(BytecodeGen.java:252)at com.google.inject.internal.BytecodeGen.newFastClassForMember(BytecodeGen.java:203)at com.google.inject.internal.DefaultConstructionProxyFactory.create(DefaultConstructionProxyFactory.java:53)at com.google.inject.internal.ProxyFactory.create(ProxyFactory.java:158)at com.google.inject.internal.ConstructorInjectorStore.createConstructor(ConstructorInjectorStore.java:90)at com.google.inject.internal.ConstructorInjectorStore.access$000(ConstructorInjectorStore.java:29)at com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:37)at com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:33)at com.google.inject.internal.FailableCache$1.load(FailableCache.java:37)at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)... 32 moreCaused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @d35dea7at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)at com.google.inject.internal.cglib.core.$ReflectUtils$1.run(ReflectUtils.java:52)at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)at com.google.inject.internal.cglib.core.$ReflectUtils.(ReflectUtils.java:42)... 48 moreProcess finished with exit code 1
原因:这是由于 JDK 8 中有关反射相关的功能自从 JDK 9 开始就已经被限制了,为了兼容原先的版本,需要在运行项目时添加--add-opens java.base/java.lang=ALL-UNNAMED
选项来开启这种默认不被允许的行为。
解决方案:
结果: