我刚开始学习spring4。在尝试运行我的hello world程序时出现以下错误,
Aug 22, 2015 9:37:17 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2b650cea: startup date [Sat Aug 22 09:37:17 IST 2015]; root of context hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldBean' defined in class com.websystique.spring.configuration.HelloWorldConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.websystique.spring.domain.HelloWorld com.websystique.spring.configuration.HelloWorldConfig.helloWorld()] threw exception; nested exception is java.lang.ClassCastException: com.websystique.spring.domain.HelloWorldImpl cannot be cast to com.websystique.spring.domain.HelloWorld
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.websystique.spring.AppMain.main(AppMain.java:12)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.websystique.spring.domain.HelloWorld com.websystique.spring.configuration.HelloWorldConfig.helloWorld()] threw exception; nested exception is java.lang.ClassCastException: com.websystique.spring.domain.HelloWorldImpl cannot be cast to com.websystique.spring.domain.HelloWorld
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
... 13 more
Caused by: java.lang.ClassCastException: com.websystique.spring.domain.HelloWorldImpl cannot be cast to com.websystique.spring.domain.HelloWorld
at com.websystique.spring.configuration.HelloWorldConfig.helloWorld(HelloWorldConfig.java:16)
at com.websystique.spring.configuration.HelloWorldConfig$$EnhancerBySpringCGLIB$$3c68b28f.CGLIB$helloWorld$0(<generated>)
at com.websystique.spring.configuration.HelloWorldConfig$$EnhancerBySpringCGLIB$$3c68b28f$$FastClassBySpringCGLIB$$50c0f2d6.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
at com.websystique.spring.configuration.HelloWorldConfig$$EnhancerBySpringCGLIB$$3c68b28f.helloWorld(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
... 14 more
我的代码如下,
配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import com.websystique.spring.domain.HelloWorld;
import com.websystique.spring.domain.HelloWorldImpl;
@Configuration
public class HelloWorldConfig {
@Bean(name="helloWorldBean")
@Description("This is a sample HelloWorld Bean")
public HelloWorld helloWorld() {
return (HelloWorld) new HelloWorldImpl();
}
}
接口
package com.websystique.spring.domain;
public interface HelloWorld {
void sayHello(String name);
}
实施班级
package com.websystique.spring.domain;
public class HelloWorldImpl {
public void sayHello(String name) {
System.out.println("Hello "+name);
}
}
主要类
public class AppMain {
public static void main(String args[]) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");
bean.sayHello("Spring 4");
context.close();
}
}
任何人都可以告诉我错误来自何处以及如何解决错误。
也可以任何人请建议我尽可能早地学习春天的好博客或文档。
答案 0 :(得分:4)
nested exception is java.lang.ClassCastException: com.websystique.spring.domain.HelloWorldImpl cannot be cast to com.websystique.spring.domain.HelloWorld
return (HelloWorld) new HelloWorldImpl();
要实现这一目标,您的HelloWorldImpl
应该实施HelloWorld
,而不是您甚至不需要将其投放到HelloWorld