我正在关注Spring的教程点,我正在尝试基于Java的配置示例。我有与教程相同的代码,但我不明白为什么标记NullPointerException。
这是我的模特
package com.tutorialspoint3.javaConfig;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Your message: " + message);
}
}
我的配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//Configuration class
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
和我的主要
package com.tutorialspoint3.javaConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp3 {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
这是堆栈错误:
Exception in thread "main" java.lang.NullPointerException
at org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
at org.springframework.core.$Proxy0.hashCode(Unknown Source)
at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:273)
at org.springframework.core.ResolvableType.hashCode(ResolvableType.java:811)
at org.springframework.util.ConcurrentReferenceHashMap.getHash(ConcurrentReferenceHashMap.java:214)
at org.springframework.util.ConcurrentReferenceHashMap.getReference(ConcurrentReferenceHashMap.java:246)
at org.springframework.util.ConcurrentReferenceHashMap.get(ConcurrentReferenceHashMap.java:226)
at org.springframework.core.ResolvableType.forType(ResolvableType.java:1207)
at org.springframework.core.ResolvableType.forType(ResolvableType.java:1177)
at org.springframework.core.ResolvableType.forTypes(ResolvableType.java:856)
at org.springframework.core.ResolvableType.getInterfaces(ResolvableType.java:403)
at org.springframework.core.ResolvableType.as(ResolvableType.java:365)
at org.springframework.core.convert.support.GenericConversionService.getRequiredTypeInfo(GenericConversionService.java:286)
at org.springframework.core.convert.support.GenericConversionService.addConverterFactory(GenericConversionService.java:118)
at org.springframework.core.convert.support.DefaultConversionService.addScalarConverters(DefaultConversionService.java:86)
at org.springframework.core.convert.support.DefaultConversionService.addDefaultConverters(DefaultConversionService.java:67)
at org.springframework.core.convert.support.DefaultConversionService.<init>(DefaultConversionService.java:54)
at org.springframework.core.env.AbstractPropertyResolver.<init>(AbstractPropertyResolver.java:41)
at org.springframework.core.env.PropertySourcesPropertyResolver.<init>(PropertySourcesPropertyResolver.java:41)
at org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:112)
at org.springframework.core.env.StandardEnvironment.<init>(StandardEnvironment.java:54)
at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.getOrCreateEnvironment(AnnotatedBeanDefinitionReader.java:177)
at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.<init>(AnnotatedBeanDefinitionReader.java:66)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:61)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:82)
at com.tutorialspoint3.javaConfig.MainApp3.main(MainApp3.java:11)
我缺少什么?我在jre 1.7下运行,Spring版本是4.1.4。如果我在jdk 1.8下运行,我收到了错误
java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.getMethod(Ljava /郎/类; Ljava /郎/字符串; [Ljava /郎/类;)Ljava /郎/反映/方法;