java – 为什么运行这个简单的Spring配置需要一个no-args构造函数?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 为什么运行这个简单的Spring配置需要一个no-args构造函数?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8873字,纯文字阅读大概需要13分钟。
内容图文
![java – 为什么运行这个简单的Spring配置需要一个no-args构造函数?](/upload/InfoBanner/zyjiaocheng/818/3db106f6f5df492aa8578926f11f0f73.jpg)
我试图在Mkyong关于Spring的教程.我创建了两个简单的类,Customer和Person,看起来像这样:
顾客:
public class Customer {
private Person person;
public Customer() {
}
//Getters and setters for person here
public String toString() {
return "Customer [person=" + person +"]";
}
}
人:
public class Person {
private String name;
private String address;
private int age;
public Person() {
}
public Person(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
//All getters and setters
public String toString() {
return "Person [address=" + address + ", age=" + age + ", name=" + name + "]";
}
}
然后我创建了一个Bean配置文件,使用内部bean(这就是我参加教程的原因),看起来像这样:
// Standard bean declarations
<bean id="CustomerBean" class="com.andrew.SpringInnerBeans.Customer">
<property name="person">
<bean class="com.andrew.SpringInnerBeans.Person">
<property name="name" value="Andrew" />
<property name="address" value="Address" />
<property name="age" value="27" />
</bean>
</property>
</bean>
</beans>
最后,我创建了一个MainApp来运行这段代码:
public static void main (String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
"NewBean.xml"});
Customer cust = (Customer) context.getBean("CustomerBean");
System.out.println(cust);
}
现在,根据我的理解,这就是发生的事情:
加载xml文件,ID为CustomerBean的bean存储在引用cust(它是Customer对象)中. CustomerBean接受一个参数“person”,并且创建Person的三个参数的详细信息作为customerBean的内部bean提供.
在MainApp中,调用引用cust的toString方法并显示正确的输出.
由于我在我的示例中专门提供了引用,为什么运行此代码需要一个无参数构造函数?如果我删除空Customer或空Person构造函数,代码将失败.为什么它在运行时失败,当我设置的bean不需要无参数构造函数?
编辑:
代码传递示例:
Customer [person=Person [address=4 Ascot House, age=27, name=Andrew]]
代码失败的示例:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomerBean' defined in class path resource [NewBean.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.andrew.SpringInnerBeans.MainApp.main(MainApp.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
... 13 more
Caused by: java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
... 14 more
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomerBean' defined in class path resource [NewBean.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.andrew.SpringInnerBeans.MainApp.main(MainApp.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.andrew.SpringInnerBeans.Customer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
... 13 more
Caused by: java.lang.NoSuchMethodException: com.andrew.SpringInnerBeans.Customer.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
... 14 more
解决方法:
再看看你从Spring容器的角度问什么.
>找到了一个Customer类的bean,只有属性:ok使用no-arg构造函数创建它作为新的Customer()并保留它.
>找到一个只有Person属性的bean的bean:ok将它创建为新的Person(),仍然使用no-arg构造函数,给它一个任意的名字并暂时保留它
>使用setter填充Person bean及其属性:确定它已准备就绪
>使用setter和Person bean作为参数填充Customer bean的唯一属性:ok它已准备就绪
>很好,应用程序上下文现已完全填充
因此,您从应用程序上下文中获取bean并且在您自己的代码中没有新的bean,但是Spring已经为您构建了它们,并且确实使用了无参数构造函数.
你可以改为举例:
public class Customer {
private Person person;
public Customer(Person person) {
this.person = person;
}
...
}
你的XML必须是:
<bean id="CustomerBean" class="com.andrew.SpringInnerBeans.Customer">
<constructor-arg>
<bean class="com.andrew.SpringInnerBeans.Person">
<property name="name" value="Andrew" />
<property name="address" value="Address" />
<property name="age" value="27" />
</bean>
</constructor-arg>
</bean>
这样,Spring将首先构建如上所述的Person bean,并使用一个arg构造函数创建Customer bean作为新Customer(person).
内容总结
以上是互联网集市为您收集整理的java – 为什么运行这个简单的Spring配置需要一个no-args构造函数?全部内容,希望文章能够帮你解决java – 为什么运行这个简单的Spring配置需要一个no-args构造函数?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。