(报错解决)No bean class specified on bean definition

关键词

spring Java eclipse bean 多例模式 prototype 懒加载 lazy-init

写在前面

  • 功能简述:对于多例模式的懒加载配置是否生效的测试。
  • 刚接触,摸索中,低级错误,只是做个记录,轻喷。

(有错误)Xml配置文件(片段)

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd ">			
		<!-- 对于多例模式,即便加了懒加载配置也没用 -->
		<bean id="myFactoryPrototypeExample" class="instance.BeanInstanceFactory"
			scope="prototype" lazy-init="false"/>
			
		<bean id="instanceFactoryInstancePrototypeExample" factory-bean="myFactoryPrototypeExample" 
			scope="prototype" lazy-init="false"/>
</beans>

运行的测试代码

package test;

import java.util.Calendar;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import instance.BeanClass;
import instance.Person;
import instance.User;

public class TestInstance {	
	//对于多例模式,默认懒加载,即便设置lazy-init="false"也无效
	@Test
	public void test06(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println("获取对象前");
		//参数是xml中待创建的对象的id
		BeanClass bc3 = (BeanClass)context.getBean("instanceFactoryInstancePrototypeExample");
		System.out.println(bc3+bc3.message);	
	}
}

解决

  • 思路:检查xml配置文件
  • 原因:xml配置文件有误。
  • 解决:最后一个bean标签少写了factory-method属性。

(正确的)Xml配置文件(片段)

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd ">			
		<!-- 对于多例模式,即便加了懒加载配置也没用 -->
		<bean id="myFactoryPrototypeExample" class="instance.BeanInstanceFactory"
			scope="prototype" lazy-init="false"/>


		<!--下面这里少写了factory-method-->
		<bean id="instanceFactoryInstancePrototypeExample" factory-bean="myFactoryPrototypeExample"
			factory-method="createBeanClassInstance" scope="prototype" lazy-init="false"/>
</beans>

包结构

在这里插入图片描述

(补充)BeanInstanceFactory类

package instance;

public class BeanInstanceFactory {
		public BeanInstanceFactory(){
			System.out.println("我是一个实例工厂");			
		}
		public BeanClass createBeanClassInstance(){
			return new BeanClass("调用实例工厂方法实例化Bean");
		}
}

(补充)BeanClass类

package instance;

public class BeanClass {
	public String message;	
	public BeanClass() {
		this.message = "构造方法实例化Bean";
	}
	public BeanClass(String message) {
		this.message = message;
	}
}

运行结果

在这里插入图片描述

参考

org.springframework.beans.factory.BeanCreationException:No bean class specified on bean definition

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值