初学者不使用XML配置Spring

时间:2012-05-28 17:49:18

标签: java spring

我对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)

1 个答案:

答案 0 :(得分:0)

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

  1. Put @EnableWebMvc注释定义此类包含Spring Framework的MVC配置。

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

  3. 据我所知你缺乏这些观点。

    希望这会对你有所帮助。欢呼声。