1

我对 Java 和 Eclipse 相当熟悉,我想我会学习一些关于 Spring 以及如何在没有 XML 的情况下配置 Spring 的知识。现在当我尝试跑步时,我得到了异常

May 28, 2012 8:08:33 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@173e55db: startup date [Mon May 28 08:08:33 PDT 2012]; root of context hierarchy
Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: MessageConfig
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:346)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:620)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)

我的课程非常简单:

public class SpringMain {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MessageConfig.class);
        DefaultMessage dmsg = context.getBean("defaultMessage", DefaultMessage.class);
        System.out.println(dmsg);
        }
    }

@Configuration
public class MessageConfig {
    @Bean
    public DefaultMessage defaultMessage() { return new DefaultMessage(); }
}

public class DefaultMessage {
    public DefaultMessage() {}

    private String message = "Spring is here.";

    public String getMessage() { return message; }

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

    @Override
    public String toString() {
        return String.format("DefaultMessage[%s]", message);
    }
}

我添加了几乎所有看起来相关的 jar 文件。我的类路径:

org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
commons-logging-1.1.1.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.aspects-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.instrument.tomcat-3.1.1.RELEASE.jar
org.springframework.instrument-3.1.1.RELEASE.jar
org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.jms-3.1.1.RELEASE.jar
org.springframework.orm-3.1.1.RELEASE.jar
org.springframework.oxm-3.1.1.RELEASE.jar
org.springframework.test-3.1.1.RELEASE.jar
org.springframework.transaction-3.1.1.RELEASE.jar
org.springframework.web.portlet-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web.struts-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
cglib-2.2.2.jar
asm-4.0.jar
commons-codec.jar
ivy.jar
jets3t.jar
commons-httpclient.jar

那么我错过了什么?为什么无法“加载配置类MessageConfig”?我正在使用 jre7 和 spring 3.1.1。

感谢您的任何建议!

[编辑]

JigarJoshi 建议我添加调用堆栈的其余部分。这里是:

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.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(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:136)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:109)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:336)
4

1 回答 1

0

我已经成功创建了一个没有任何 xml 配置的 Web 应用程序。我建议您在MessageConfig类中执行以下操作。

  1. Put@EnableWebMvc注解定义这个类包含 Spring Framework 的 MVC 配置。

  2. extends WebMvcConfigurerAdapter这将使您覆盖一些有用的功能,例如addResourceHandlers(ResourceHandlerRegistry registry)

据我说,你缺乏这些点。

希望这对您有所帮助。干杯。

于 2012-05-29T05:28:42.797 回答