【问题标题】:Cannot load configuration class无法加载配置类
【发布时间】:2014-10-24 11:42:53
【问题描述】:

我正在关注 this 关于如何使用 Spring 的教程,根据提供的示例,我得到以下异常:

Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: com.tutorialspoint.HelloWorldConfig
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:378)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:263)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:126)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:128)
    at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:100)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:368)
    ... 7 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
    ... 12 more
Caused by: java.lang.SecurityException: class "com.tutorialspoint.HelloWorldConfig$$EnhancerBySpringCGLIB$$b5aece24"'s signer information does not match signer information of other classes in the same package
    at java.lang.ClassLoader.checkCerts(ClassLoader.java:952)
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:666)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:794)
    ... 18 more

我研究了我的问题并找到了this;有人也遇到了和我一样的问题,这与确保 ASM 与 CGLIB 兼容有关。但是我已经尝试过这个解决方案,但它没有奏效,我什至使用了与提供的版本完全相同的版本(GBLIB 2.2.2 和 ASM 3.3.1)。

我需要做什么来纠正这个问题?


为简单起见,以下是我正在使用的文件,这些文件是从提供的教程中提取的。

HelloWorldConfig.java

package com.tutorialspoint;
import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {

    @Bean
    public HelloWorld helloWorld() {
        return new HelloWorld();
    }
}

HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Your Message : " + message);
    }
}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class MainApp {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext ctx = new AnnotationConfigApplicationContext(
                HelloWorldConfig.class);

        HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

        helloWorld.setMessage("Hello World!");
        helloWorld.getMessage();
    }
}

另外说“但是我已经尝试过这个解决方案但它没有工作”我的意思是返回完全相同的错误。

【问题讨论】:

  • 您使用的是什么版本的 Java?这在使用像 ASM 这样的字节码操作库时会有所不同。
  • 春季版?他们现在正在遮蔽一些字节码操作。
  • @drembert JavaSE-1.7
  • @chrylis 抱歉,那是 Spring Tool Suite 版本,我正在使用的 IDE 以使用 Spring。 Spring 版本为 4.0.2。

标签: java spring


【解决方案1】:

所以,我想说你提到的“其他”有一个不同的问题。
即使“最后显示的异常”与您的相同。
但正如您在堆栈跟踪中看到的那样,“源”是 SecurityException

*Cannot load configuration class*-Error 是后遗症

我认为您的项目中的“代码签名”有问题
或者,由于 ByteCode-Manipulation,签名被破坏。

PS
有时,当您在项目中引用“SignedLibs”“UnsignedLibs”时,也会发生这种情况。
在这种情况下,从已签名的库中删除签名。

【讨论】:

  • 如何从已签名的库中删除签名?
  • 使用任何 Zip-Archiver 打开 Jar,编辑 Manifest 文件(Meta-Inf 文件夹)并删除 Java 类具有签名信息的所有行...此外,您可以删除Meta-Inf 文件夹中的 *.rsa 文件。
  • 签名信息是什么样的?像这样? Name: org/springframework/context/weaving/AspectJWeavingEnabler.class SHA1-Digest: l50iHSxP9TMDMpK9ncKZNZuDX4M=
  • 是的,删除所有这些条目以删除签名信息。同时删除 meta-inf 目录中的 sf 和 dsa 文件
  • 好的,我已经完成了,但是系统现在声明The import org.springframework.context cannot be resolved
【解决方案2】:

运行此项目所需的所有 jar:
1) org.springframework.core-3.0.1.RELEASE-A.jar
2) spring-context-3.0.4.RELEASE.jar
3) org.springframework.beans-3.0.1.RELEASE-A.jar
4) commons-logging-1.1.1.jar
5) asm-3.3.1.jar
6) cglib-2.2.2.jar

要获取这些 jars,可以将下载的 jars 直接添加到您的项目中,或者在 pom.xml 中提供以下依赖项以自动为您下载它们。

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>

如果不存在,请将其添加到您的 maven settings.xml 文件中:

    <profiles>
        <profile>
            <id>SPRINGLEARN</id>
            <activation>
                <jdk>1.8</jdk>
            </activation>
            <repositories>
                <repository>
                    <id>thirdPartyRepo</id>
                    <name>Third party repository</name>
                    <url>https://repo.spring.io/libs-release/</url>
                    <layout>default</layout>
                    <snapshotPolicy>always</snapshotPolicy>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>thirdPartyPluginRepo</id>
                    <name>Third party plugin repository</name>
                    <url>https://repo.spring.io/libs-release/</url>
                    <layout>default</layout>
                    <snapshotPolicy>always</snapshotPolicy>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

在此之后,只需运行您的项目。
- 右键单击​​您的项目 -> 运行方式 -> Maven clean
- 右键单击​​您的项目 -> 运行方式 -> Maven 安装
- 右键单击​​您的项目 -> 运行方式 -> Java 应用程序

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 2012-12-07
    • 1970-01-01
    • 2021-10-02
    • 2013-08-10
    • 2016-10-26
    • 2023-02-23
    • 2021-03-21
    相关资源
    最近更新 更多