【问题标题】:java.lang.IllegalArgumentException with AnnotationConfigApplicationContextjava.lang.IllegalArgumentException 与 AnnotationConfigApplicationContext
【发布时间】:2018-12-31 20:16:38
【问题描述】:

我正在尝试遵循链接https://www.tutorialspoint.com/spring/spring_java_based_configuration.htm 中@Configuration 和@Bean Annotations 部分下的示例,但出现以下异常:

Dec 31, 2018 2:59:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2dda6444: startup date [Mon Dec 31 14:59:33 EST 2018]; root of context hierarchy
Exception in thread "main" java.lang.IllegalArgumentException
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
    at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:139)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:282)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
    at com.tutorialspoint.MainApp.main(MainApp.java:9)

我使用的是 java 1.8,spring 3.2.1。感谢您的帮助。

package com.tutorialspoint;
import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {
   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

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

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

在 MainApp.java 的新 AnnotationConfigApplicationContext(HelloWorldConfig.class) 上失败

【问题讨论】:

  • 您能否更新代码并更好地解释您在哪一步得到了这个
  • 尝试给出完整路径new AnnotationConfigApplicationContext(com.tutorialspoint.HelloWorldConfig.class);
  • 感谢您的回复。它在加载类时确实显示了安全问题。我尝试包含包名,但错误并没有消失。

标签: java spring


【解决方案1】:

如果可以,请按照教程中的说明使用 Spring 4.1.6。

我使用 spring-framework-3.2.1.RELEASE-dist.zip 重现了您的错误,但使用 spring-framework-4.1.6.RELEASE-dist.zip 的 Hello World 应用对我来说运行良好。

【讨论】:

  • 感谢您的建议。我用的是最新版本,效果很好。
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多