2012-05-28 71 views
1

我對Java和Eclipse非常熟悉,我想我會學習一些關於Spring的知識以及如何在沒有XML的情況下配置Spring。當我嘗試運行,現在,我得到的異常初學者在沒有XML的情況下配置Spring

May 28, 2012 8:08:33 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing org.spring[email protected]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

請問您能否添加異常堆棧跟蹤的根本原因? –

+1

另外我建議不要包括什麼是基本隨機庫。 –

+0

@JigarJoshi:不確定你的意思。當我嘗試獲取AnnotationConfigApplicationContext的實例時,我得到了崩潰。 我同意戴夫牛頓。我從一大堆圖書館開始,並加入越來越多的妄圖殺死這個例外的東西。 – Russ

回答

0

我已經成功創建了一個沒有任何xml配置的web應用程序。我建議您在您的MessageConfig類中執行以下操作。

  1. put @EnableWebMvc註解定義該類保存Spring Framework的MVC配置。

  2. extends WebMvcConfigurerAdapter,可以讓你忽略了一些有用的功能,如addResourceHandlers(ResourceHandlerRegistry registry)

據我你缺乏這幾點。

希望這可以幫助你。乾杯。

相關問題