我从Spring reference 3.0开始学习spring,我想尝试如何实例化内部bean:
这是我的代码:
package com.springexample;
public class ExampleBean {
private String samplePropertyExampleBean;
public void setSamplePropertyExampleBean(String samplePropertyExampleBean) {
this.samplePropertyExampleBean = samplePropertyExampleBean;
}
public String getSamplePropertyExampleBean() {
return samplePropertyExampleBean;
}
class InnerBean{
private String sampleProperty;
public void setSampleProperty(String sampleProperty) {
this.sampleProperty = sampleProperty;
}
public String getSampleProperty() {
return sampleProperty;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的配置文件是:
当我试图检索bean InnerBean时,我收到以下错误:
线程"main"中的异常org.springframework.beans.factory.BeanCreationException:创建类路径资源[spring-config.xml]中定义名为'InnerBean'的bean时出错:bean的实例化失败; 嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.springexample.ExampleBean $ InnerBean]:找不到默认构造函数; 嵌套异常是java.lang.NoSuchMethodException:com.springexample.ExampleBean $ InnerBean.()
可能是什么问题呢?我尝试在InnerBean中添加无参数构造函数仍然我收到错误..
谁能帮我?
Boz*_*zho 21
这是Java的一个警告 - 内部类默认构造函数不是无参数构造函数.它们的默认构造函数采用1个类型的参数 - 外部类.
所以,<constructor-arg>用来传递一个类型的beanExampleBean
当然,只有在必要时才使用非静态内部类.如果课程只是你所展示的,那就去做吧static.或者将其移动到新文件.那你就没有上述限制了.首选静态内部类是一种很好的做法,不仅适用于spring bean,也适用于Java.
| 归档时间: |
|
| 查看次数: |
7266 次 |
| 最近记录: |