首页 > 其他分享 >配置问题-Error creating bean with name 'user' defined in class path resource [bean.xml]

配置问题-Error creating bean with name 'user' defined in class path resource [bean.xml]

时间:2023-07-14 21:22:57浏览次数:52  
标签:xml resource java support springframework bean org class

正在学习 IoC

使用的 jdk 版本为 jdk 17

依赖为:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>6.0.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.9</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.2</version>
    </dependency>
</dependencies>

报错内容如下:

7月 14, 2023 9:04:39 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [bean.xml]: No bean class specified on bean definition
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [bean.xml]: No bean class specified on bean definition
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1314)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1199)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.study.spring6.iocxml.UserTest.main(UserTest.java:8)
Caused by: java.lang.IllegalStateException: No bean class specified on bean definition
	at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:428)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1308)
	... 13 more

报错文件配置:

<bean id="user" name="com.study.spring6.iocxml.User"/>

搜了一下出现 bug 的原因:https://stackoverflow.com/questions/20544963/java-lang-illegalstateexception-no-bean-class-specified-on-bean-definition

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 属性中指定要实例化的对象类型(或类)。该 class 属性在内部是 BeanDefinition 实例的 Class 属性,通常是强制性的。


分析原因:

没有配置 class


解决:

添加上 class 属性

<bean id="user" class="com.study.spring6.iocxml.User"/>

标签:xml,resource,java,support,springframework,bean,org,class
From: https://www.cnblogs.com/ShaunY/p/17555015.html

相关文章

  • Spring源码剖析-BeanFactory和ApplicationContext(其一)
    1.在Spring中,BeanFactory和ApplicationContext是两种容器,它们之间的关系是怎样的?我们使用的是哪个?它们的底层原理是什么?以一个SpringBoot的启动类为例:packagecom.example.demo3;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.auto......
  • day118 - 基于xml管理bean的入门案例
    基于xml管理bean入门案例导入依赖<dependencies><!--基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId>......
  • spring bean 的属性为 java.util.Properties 时如何初始化该属性
       publicclassFooBean{privatejava.util.Propertiesattr;publicjava.util.PropertiesgetAttr(){returnattr;}publicvoidsetAttr(java.util.Propertiesattr){this.attr=attr;}} <beanid=......
  • defined in class path resource [de/codecentric/boot/admin/server/config/AdminSe
    搭建springbootadmin监控时出现的,经排查是版本过低,换个高版本的,顺利进入网页界面<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.7.10</version......
  • bean加载控制
            ......
  • 使用nuxtjs静态部署时报错TypeError: renderContext.renderResourceHints is not a fu
    在使用nuxt框架进行静态打包部署时候每次总会有一两个界面报TypeError:renderContext.renderResourceHintsisnotafunction的错误 一开始以为是asyncData中获取的数据有问题,在各种排查后返现数据没问题,再次编译还是会有错误,最后甚至把asyncdata方法给删除掉都还是有这个错......
  • java 加载bean
    @Service@AutowiredprivateActionLogAspectactionLogAspect;@RestController@RequiredArgsConstructorprivatefinalImComplaintServiceimComplaintService;@Slf4j@RequiredArgsConstructor@ComponentfinalActionLogAspectactionLogAspect;......
  • 《系列二》-- 10、initialize-初始化bean
    目录initializeBean方法源码如下二、重要操作2.1应用Aware2.2applyBeanPostProcessorsBeforeInitialization:2.3invokeInitMethods:2.4applyBeanPostProcessorsAfterInitialization阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需要看官自......
  • 《系列二》-- 9、bean属性填充
    目录一、概述:populateBean在什么时候执行?二、populateBean的重要操作三、重点操作一propertyValue的注入3.1根据Bean名称注入3.2浅看一下,获取非'简单'类型property的方法3.3根据Bean类型注入四、注入依赖的应用阅读之前要注意的东西:本文就是主打流水账式的源码阅......
  • Bean的实例化
    Bean实例化一.构造方法进行实例化bookDaoImplpackagedang.dao.impl;importdang.dao.BookDao;publicclassBookDaoImplimplementsBookDao{publicBookDaoImpl(){System.out.println("bookdaoconstructorisrunning....");}publicv......