无法使用Spring创建Innerbean - BeanInstantiationException找不到默认构造函数 [英] Failing to create Innerbean using Spring - BeanInstantiationException No default constructor found

查看:996
本文介绍了无法使用Spring创建Innerbean - BeanInstantiationException找不到默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Spring reference 3.0开始学习spring,我想尝试如何实例化内部bean:

I started learning spring from Spring reference 3.0 and i wanted to try how to instantiate inner 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;
  }

 }


}

我的配置文件是:



当我尝试检索bean InnerBean时,我收到以下错误:

When i am trying to retrieve the bean InnerBean i am getting the following error:

线程main中的异常org.springframework.beans.factory.BeanCreationException:创建bean时出错在类路径资源[spring-config.xml]中定义名称为'InnerBean':bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.springexample.ExampleBean $ InnerBean]:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:com.springexample.ExampleBean $ InnerBean。()

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'InnerBean' defined in class path resource [spring-config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.springexample.ExampleBean$InnerBean]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springexample.ExampleBean$InnerBean.()

可能是什么问题?我尝试在InnerBean中添加无参数构造函数,但仍然收到错误..

What could be the problem? I tried adding no-argument constructor in the InnerBean still i am getting error..

任何人都可以帮助我吗?

Can any one help me?

推荐答案

这是Java的一个警告 - 内部类默认构造函数不是无参数构造函数。它们的默认构造函数接受1个类型的参数 - 外部类。

That's one caveat of Java - inner classes default constructor isn't a no-arg constructor. Their default constructor takes 1 parameter of type - the outer class.

因此,使用< constructor-arg> 传递类型为的Bean> ExampleBean

So, use <constructor-arg> to pass a bean of type ExampleBean

当然,只有在必要时才使用非静态内部类。如果该类仅是您显示的类,则将其设为 static 。或者将其移动到新文件。那你就没有上述限制了。首选静态内部类不仅适用于spring bean,而且适用于Java。

Of course, use non-static inner classes only if this is necessary. If the class is only what you have shown, then make it static. Or move it to a new file. Then you won't have the above restriction. Preferring static inner classes is a good practice not only for spring beans, but in Java in general.

这篇关于无法使用Spring创建Innerbean - BeanInstantiationException找不到默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆