当我使用带注释的 Spring (而非XML)时,我无法在此网站上找到解决以下问题的解决方案。
有什么建议我做错了吗?
我有以下简单的课程。我不会把它扩展到其他地方。
@Component
@Scope("prototype")
public class Matryoshka
{
private Matryoshka[] matryoshki;
public Matryoshka(Matryoshka[] matryoshki)
{
this.matryoshki = matryoshki;
}
}
然后我尝试用Spring <T> T BeanFactory.getBean(Class<T> var1, Object... var2)
在Spring扫描课程中,我做了类似的事情:
Matryoshka matryoshka = new Matryoshka(new Matryoshka[] {});
matryoshka = applicationContext.getBean(matryoshka.getClass(), new Matryoshka[] {matryoshka, matryoshka});
//matryoshka = (Matryoshka) applicationContext.getBean("matryoshka", new Matryoshka[] {matryoshka, matryoshka}); //also gives same error
错误信息是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'matryoshka' defined in file [/home/...../Matryoshka.class]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
更新
当我尝试实例化的bean的构造函数具有固定数量的参数时,看起来BeanFactory.getBean(Class<T> var1, Object... var2)
有效。例如。
public Matryoshka(Matryoshka matryoshki1, Matryoshka matryoshki2)
,
但不是public Matryoshka(Matryoshka[] matryoshki)
或public Matryoshka(Matryoshka... matryoshki)
。
是否可以使Spring使用具有无限数量参数的构造函数?
我在下面的回答中看到过类似的东西(不是构造函数)
但是他们使用了XML配置。