java 无法加载配置类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25585601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:16:40  来源:igfitidea点击:

Cannot load configuration class

javaspring

提问by Kurtiss

I am following thistutorial about how to use Spring and based on the provided example, I get the following exception:

我正在关注有关如何使用 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

I have researched my problem and have found this; someone also has had the same problem as me, and it has something to do with ensuring that ASM is compatible with CGLIB. However I have tried this solution and it has not worked, I even went as far as using the exact same versions as the one provided (GBLIB 2.2.2 and ASM 3.3.1).

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

What do I need to do in order to correct this?

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



For simplicity, here are the files which I am using that were extracted from the provided tutorial.

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

HelloWorldConfig.java

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();
    }
}

Also by saying 'However I have tried this solution and it has not worked' I mean that the exact same error is returned.

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

回答by Ben

So, i would say the "other" you mentioned, has an different problem.
Even when the "Last-Shown-Exception" is the same as yours.
But as you can see in your stacktrace, the "source" is a SecurityException.

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

The *Cannot load configuration class*-Error is a aftereffect

*无法加载配置类*-错误是后遗症

I assume there is something wrong with the "code signation" in your project
or, due to ByteCode-Manipulation, the signation is broken.

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

PS:
Sometimes this also can happen, when you reference "SignedLibs" and"UnsignedLibs" in your project.
in this case remove the signation from the signed libs.

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

回答by User

All jars required for this project to run:
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:
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

To get these jars,either add the downloaded jars to your project directly, or provide the following dependencies in the pom.xml to get them automatically downloaded for you.

要获取这些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>

Add this to your maven settings.xml file if not already present:

如果尚未存在,请将其添加到您的 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>

After this, just run your project.
-Right click on your project -> Run as -> Maven clean
-Right click on your project -> Run as -> Maven install
-Right click on your project -> Run as -> Java application

在此之后,只需运行您的项目。
-右键单击您的项目 -> Run as -> Maven clean
-右键单击您的项目 -> Run as -> Maven install
-右键单击您的项目 -> Run as -> Java 应用程序