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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20544963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:40:46  来源:igfitidea点击:

java.lang.IllegalStateException: No bean class specified on bean definition

javaspring

提问by Mercenary

Trying to run a simple spring based program. I'm using spring-framework-3.0.5. Not sure what is causing this error!!

尝试运行一个简单的基于 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>

Sample class:

示例类:

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

Exception got :

异常得到:

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.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)

采纳答案by Sotirios Delimanolis

The documentationexplains

文件解释

A bean definition essentially is a recipe for creating one or more objects. The container looks at the recipe for a named bean when asked, and uses the configuration metadata encapsulated by that bean definition to create (or acquire) an actual object.

If you use XML-based configuration metadata, you specify the type (or class) of object that is to be instantiated in the class attribute of the element. This class attribute, which internally is a Class property on a BeanDefinition instance, is usually mandatory.

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

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

Your bean definition is missing the classattribute. Add it

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

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

回答by Philip Rego

If you're using Spring MVC ResourceBundleViewResolver, and your view beans are not written right in view.properties you will get this error.

如果您使用的是 Spring MVC ResourceBundleViewResolver,并且您的视图 bean 未正确写入 view.properties,您将收到此错误。

See this link to help verify your view beans are correct. https://www.mkyong.com/spring-mvc/spring-mvc-resourcebundleviewresolver-example/

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