java.lang.ClassNotFoundException: com.google.inject.AbstractModule 未被 org.mybatis.mybatis-guice 找到

java.lang.ClassNotFoundException: com.google.inject.AbstractModule not found by org.mybatis.mybatis-guice

存在使用felix框架使用mybatis-guice库的bundle无法启动的问题。 运行环境是java11和karaf 4.2.14版本

这是相应的错误信息。


Error when instantiating bean daoService of class myApp.dao.inject.impl.ServiceImpl
org.osgi.service.blueprint.container.ComponentDefinitionException: Error when instantiating bean daoService of class myApp.dao.inject.impl.ServiceImpl
            at org.apache.aries.blueprint.container.BeanRecipe.wrapAsCompDefEx(BeanRecipe.java:362)
            at org.apache.aries.blueprint.container.BeanRecipe.getInstanceFromType(BeanRecipe.java:352)
            at org.apache.aries.blueprint.container.BeanRecipe.getInstance(BeanRecipe.java:283)
            at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:685)
            at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:666)
            at org.apache.aries.blueprint.di.AbstractRecipe.call(AbstractRecipe.java:81)
            at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
            at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:90)
            at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:360)
            at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:190)
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:737)
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:433)
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:298)
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:335)
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:288)
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:284)
            at org.apache.aries.blueprint.container.BlueprintExtender.modifiedBundle(BlueprintExtender.java:274)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)
            at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1179)
            at org.apache.felix.framework.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:730)
            at org.apache.felix.framework.EventDispatcher.fireBundleEvent(EventDispatcher.java:485)
            at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4579)
            at org.apache.felix.framework.Felix.startBundle(Felix.java:2174)
            at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
            at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)
            at org.apache.karaf.features.internal.service.BundleInstallSupportImpl.startBundle(BundleInstallSupportImpl.java:165)
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:1154)
            at org.apache.karaf.features.internal.service.Deployer.deploy(Deployer.java:1044)
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1063)
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.lambda$doProvisionInThread(FeaturesServiceImpl.java:998)
            at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
            at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.lang.NoClassDefFoundError: com/google/inject/AbstractModule
            at java.base/java.lang.ClassLoader.defineClass1(Native Method)
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2410)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2194)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1607)
            at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at java.base/java.lang.ClassLoader.defineClass1(Native Method)
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2410)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2194)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1607)
            at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1414)
            at org.apache.felix.framework.BundleWiringImpl.searchImports(BundleWiringImpl.java:1660)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1590)
            at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at java.base/java.lang.ClassLoader.defineClass1(Native Method)
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2410)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2194)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1607)
            at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at myApp.dao.inject.config.DaoModuleBindConfig$GameModule.configure(DaoModuleBindConfig.java:27)
            at com.google.inject.PrivateModule.configure(PrivateModule.java:102)
            at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:409)
            at com.google.inject.spi.Elements.getElements(Elements.java:108)
            at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:160)
            at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
            at com.google.inject.Guice.createInjector(Guice.java:87)
            at com.google.inject.Guice.createInjector(Guice.java:69)
            at com.google.inject.Guice.createInjector(Guice.java:59)
            at myApp.dao.inject.impl.ServiceImple.<init>(ServiceImple.java:17)
            at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
            at org.apache.aries.blueprint.utils.ReflectionUtils.newInstance(ReflectionUtils.java:369)
            at org.apache.aries.blueprint.container.BeanRecipe.newInstance(BeanRecipe.java:839)
            at org.apache.aries.blueprint.container.BeanRecipe.getInstanceFromType(BeanRecipe.java:350)
            \... 36 more
    Caused by: java.lang.ClassNotFoundException: com.google.inject.AbstractModule not found by org.mybatis.mybatis-guice [160]
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1639)
            at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            ... 83 more

以下是捆绑使用的依赖项和导入包的列表。

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-guice</artifactId>
    </dependency>
    
    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
            <instructions>
                <Import-Package>
                    !junit.framework*,
                    !org.junit*,
                    !org.mockito*,
                    !org.checkerframework.*,
                    org.apache.ibatis.builder.xml,
                    com.microsoft.sqlserver.jdbc,
                    *;
                </Import-Package>
                <Embed-Dependency/>
                <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                <DynamicImport>*</DynamicImport>
                <Export-Package>myApp.dao.*;version=${project.version};-noimport:=true
                </Export-Package>
                <!--suppress MavenModelInspection -->
                <_versionpolicy>[$(version;==;$(@)),$(version;+;$(@)))</_versionpolicy>
                <effective>effective:=active</effective>
            </instructions>
        </configuration>
    </plugin>

依赖项的版本信息在父 POM 中定义。

还根据 guice osgi wiki

添加了引导委托

set JAVA_OPTS=-XX:+UnlockDiagnosticVMOptions --add-modules java.se --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.management/sun.management=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED -Dhazelcast.diagnostics.enabled=true -Dorg.osgi.framework.bootdelegation=org.aopalliance.*,com.google.inject.*

写入 karaf.bat 文件。

为了以防万一,我在 karaf config.property 的 bootdelegation 中根据 karaf container 4.x - 4.14.7. Security providers

指定了包

org.osgi.framework.bootdelegation =
com.sun.*,
javax.transaction,
javax.transaction.xa,
javax.xml.crypto,
javax.xml.crypto.*,
jdk.nashorn.*,
sun.,
jdk.internal.reflect,
jdk.internal.reflect.
,
org.apache.karaf.jaas.boot.principal,
org.apache.karaf.jaas.boot,
org.aopalliance,
org.aopalliance.*,
com.google.inject,
com.google.inject.*,
com.google.common,
com.google.common.*,
org.mybatis.guice,
org.mybatis.guice.*

然而,classloader 仍然说找不到 class。

ID State Lvl Version Name
60 Active 80 5.1.0 guice
159 Active 80 3.5.9 mybatis
160 Active 80 3.17.0 mybatis-guice

确认guice、mybatis、mybatis-guice也活跃

我还能尝试什么?

预先感谢您的回复。

这部分错误信息:

com.google.inject.AbstractModule not found by org.mybatis.mybatis-guice

建议 org.mybatis.mybatis-guice 包应该导入包 com.google.inject 但没有这样做。因此,您添加到自己的包中的所有缓解措施,例如 DynamicImport(无论如何都不是有效的 OSGi header)将不会执行任何操作,因为它是 third-party 包有错。