java - java.lang.IllegalArgumentException 与 AnnotationConfigApplicationContext
问题描述
我正在尝试遵循链接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) 上失败
解决方案
如果可以,请使用教程中指定的 Spring 4.1.6。
我使用复制了您的错误,spring-framework-3.2.1.RELEASE-dist.zip
但 Hello World 应用程序对我来说运行良好spring-framework-4.1.6.RELEASE-dist.zip
。
推荐阅读
- c++ - 将 std::filesystem 头文件添加到我的程序时出现编译错误
- azure - Azure AD Premium 企业应用程序许可
- java - 在任务栏上固定使用 Netbeans 创建的 Java 程序?
- javascript - 如何用 Symfony Form 中原型生成的全新元素填充实体类型选择字段?
- python - python中是否有任何数据结构可以替代c ++中的bitset?
- hibernate - 查询检索到的重复对象
- php - Word搜索脚本修复解决方案
- javascript - 尝试使用 Fetch API 在函数中检索 json 信息,然后尝试在另一个函数中使用它们
- excel - 以模块化格式调用函数和子的问题
- c# - 使用 EPPLUS 将带有 LoadFromCollection 的列表或数组添加到行范围