java.lang.IllegalStateException:没有在bean定义上指定bean类

时间:2013-12-12 13:32:59

标签: java spring

尝试运行一个简单的基于弹簧的程序。我正在使用spring-framework-3.0.5。 不确定导致此错误的原因!!

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="user" name="com.spsam.UserPOJO">
        <property name="id" value="1351231"/>
        <property name="name" value="Ninam"/>
    </bean>
</beans>

示例类:

public class Sample1 {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserPOJO user = (UserPOJO) context.getBean("user");
        user.getUserDetails();
    }
}

有例外:

Dec 12, 2013 6:59:08 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@744607b5: startup date [Thu Dec 12 18:59:08 IST 2013]; root of context hierarchy
Dec 12, 2013 6:59:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Dec 12, 2013 6:59:08 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6e58f809: defining beans [user]; root of factory hierarchy
Dec 12, 2013 6:59:08 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6e58f809: defining beans [user]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.spsam.Sample1.main(Sample1.java:18)
Caused by: java.lang.IllegalStateException: No bean class specified on bean definition
    at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:372)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:52)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
    ... 13 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

2 个答案:

答案 0 :(得分:3)

documentation解释了

  

bean定义本质上是创建一个或多个的配方   对象。容器在查看命名bean的配方时   问,并使用该bean封装的配置元数据   创建(或获取)实际对象的定义。

     

如果使用基于XML的配置元数据,则指定类型(或   要在类的属性中实例化的对象的类   元素。 此类属性,内部为   BeanDefinition实例上的类属性通常是必需的

您的bean定义缺少class属性。添加它

<bean id="user" name="com.spsam.UserPOJO" class="com.spsam.UserPOJO">
    <property name="id" value="1351231"/>
    <property name="name" value="Ninam"/>
</bean>

答案 1 :(得分:0)

如果您使用的是Spring MVC ResourceBundleViewResolver,而您的视图bean没有正确地写在view.properties中,则会出现此错误。

请参阅此链接以帮助验证您的View Bean是否正确。 https://www.mkyong.com/spring-mvc/spring-mvc-resourcebundleviewresolver-example/