0

我正在学习 Spring 教程,我有以下代码,使用基于 Java 的配置,但它不起作用。在我的类路径中,我有 Spring 3.1 jar,以及 asm-4.0.jar 和 cglib-2.2.2.jar。

MainApp.java:

package com.tutorialspoint;

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

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(
                HelloWorldConfig.class);
        HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
        helloWorld.setMessage("Hello World!");
        helloWorld.getMessage();
    }
}

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

错误代码:

Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: com.tutorialspoint.HelloWorldConfig
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:456)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigurationClasses(ConfigurationClassPostProcessor.java:202)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:176)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:604)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:437)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:68)
    at com.tutorialspoint.MainApp.main(MainApp.java:8)
Caused by: java.lang.VerifyError: class net.sf.cglib.core.DebuggingClassWriter overrides final method visit.(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at net.sf.cglib.core.AbstractClassGenerator.<init>(AbstractClassGenerator.java:38)
    at net.sf.cglib.core.KeyFactory$Generator.<init>(KeyFactory.java:127)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:112)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer(ConfigurationClassEnhancer.java:101)
    at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:89)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:448)
    ... 6 more
4

4 回答 4

1

当我第一次遇到这个问题时,我正在使用 cglib-2.1_3.jar 和 asm 3.1.jar。在我删除 asm 3.1.jar 并添加 asm-1.5.3.jar 后,它开始工作。

于 2015-03-10T09:14:45.803 回答
1

我将 asm jar 文件从 asm-4.0.jar 更改为 asm-3.3.jar,它起作用了。第一个 asm jar 和我使用的 cglib jar 之间一定有冲突。

于 2012-07-27T18:08:08.433 回答
0

我使用的是 cglib-2.1_3.jar 和 asm-1.5.3,在使用这个 jar 之后我的程序运行成功。

  1. ASM 核心 » 1.5.3

  2. cglib-2.1_3.jar

于 2015-07-11T06:12:04.953 回答
0

只需添加此答案,以防有人遇到我遇到的奇怪的极端案例问题。

当 Jar 文件未正确签名时,也会发生这种情况。

于 2018-06-29T20:40:13.827 回答