org.springframework.beans.factory.BeanCreationException

报错信息


Error creating bean with name 'std1' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [cn.zzn.spring01.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.zzn.spring01.Student.<init>()
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
	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:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at Test.main(Test.java:9)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [cn.zzn.spring01.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.zzn.spring01.Student.<init>()
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
	... 13 more
Caused by: java.lang.NoSuchMethodException: cn.zzn.spring01.Student.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
	... 14 more

错误代码


// 类的有参构造器(覆盖了类里的默认无参构造器)
public Student(Integer number, String name) {
        this.number = number;
        this.name = name;
   }

// applicationContext.xml配置文件
	<bean id="std1" class="cn.zzn.spring01.Student">
        <property name="number" value="1001"></property>
        <property name="name" value="李华"></property>
    </bean>

// 用反射方式获取没有无参构造的类对象
Student std1 = (Student) ac.getBean("std2",Student.class);

错误分析


反射机制是用的类的无参构造器,但是我们写了有参构造又没有手动添加一个无参构造器,所以报错。

解决方式


// 1. 删除有参构造器

// 2. 添加一个无参构造器


声明:该文章由本人自学后记录,仅供参考,如有疏漏 感谢您的指正!谢绝转载!

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页