无法加载配置类

12

我正在按照这个关于如何使用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

我已经研究了我的问题,并找到了这个链接; 有人和我遇到了同样的问题,这与确保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();
    }
}

同样地,当我说“然而我尝试了这个解决方案却无效”时,我的意思是返回了完全相同的错误信息。

1
你使用的Java版本是什么?这会在使用像ASM这样的字节码操作库时产生影响。 - drembert
Spring 版本?他们现在正在进行一些字节码操作的遮蔽。 - chrylis -cautiouslyoptimistic-
@drembert JavaSE-1.7 - Kurtiss
@chrylis 抱歉,那是Spring Tool Suite版本,我正在使用它来使用Spring。Spring版本是4.0.2。 - Kurtiss
8个回答

7

我曾经遇到同样的问题,并且发现在POM.xml中设置的JRE版本或项目关联的默认版本未设置在类路径中。因此,在“首选项”下更新了相同的内容 -> “已安装的JRE”,然后运行应用程序,问题得以解决。


6

我昨天遇到了这个问题,以下是解决方法。

  1. 打开Eclipse
  2. 在菜单栏中打开窗口 -> 首选项 -> Java -> 已安装的 JRE
  3. 添加已在系统中安装的新的JRE(C:Program Files->Java->jre->bin),将其添加。
  4. 选择新添加的JRE,BOOOM!

4
This problem occurred due to spring dependency problem, I too used below dependency facing same issue, the configuration classes didn't loaded

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>

Try below one: for me it is working 

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>
    
    

0

此项目运行所需的所有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

要获取这些JAR包,您可以直接将已下载的JAR包添加到项目中,或在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 清理
-右键单击您的项目 -> 运行为 -> Maven 安装
-右键单击您的项目 -> 运行为 -> Java 应用程序


0

所以,我会说你提到的“其他”有一个不同的问题。
即使“Last-Shown-Exception”与你的相同。
但是,正如您在堆栈跟踪中看到的那样,“源”是一个SecurityException

*无法加载配置类*错误是一种后果

我认为您的项目中的“代码签名”存在问题
或者由于字节码操作,签名已损坏。

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


我该如何去除已签名库中的签名? - Kurtiss
好的,我已经做了,但是系统现在显示“无法解析导入org.springframework.context”。 - Kurtiss
我会具体说明我所做的事情,我得到了jar文件,解压缩它,进入META-INF,通过删除所有签名来编辑MANIFEST.MF,并从文件夹中删除了.RSA和.SF(没有.DSA)。然后我将其重新压缩,转换回.JAR并用其替换原始文件。我刷新了项目以使用更新的JAR,但是当我这样做时,系统返回多个错误,主要是由于它无法导入所需的JAR(“无法解析导入org.springframework.context”)。 - Kurtiss
除了异常部分,一切听起来都正确。看起来你的项目有一些依赖问题。注意:不要从清单中删除类路径信息。 - Ben
其实,以防万一,我应该在哪个JAR文件上执行这个操作? - Kurtiss
显示剩余9条评论

0

我也遇到了这个问题。 使用最新版本的Spring。在5.x版本中可以正常工作。


0

1
您已经包含了一个指向代码图片的链接,这不是在StackOverflow上分享代码的正确方式。如果您需要分享代码,请直接将其粘贴到答案中并使用代码格式化功能。 - Vladimir Kattsyn

0
我也遇到了同样的问题。我尝试使用不同版本来解决问题。 但是当我使用低于5.1.0.RELEASE的版本时,仍然会出现相同的错误... 但是对于所有从5.1.0.RELEASE版本开始的版本,它都可以正常工作。
示例代码:
 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.0.RELEASE</version>
    </dependency>

您的回答目前写得不清楚。请编辑以添加额外细节,这将有助于其他人理解如何回答问题。您可以在 帮助中心 中找到更多关于如何撰写好答案的信息。 - Community
谢谢您的建议。现在我觉得很清楚了。 - pujitha chiguluri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接