2012-05-18 84 views
0

我在模型中使用了第三方库类XYZ作为参数。 XYZ没有默认的构造函数。所以春天是不能够创建豆给它的错误信息为春天的bean类实例化没有默认构造函数的类

org.springframework.web.util.NestedServletException: Request processing failed; 

nested exception is org.springframework.data.mapping.model.MappingInstantiationException: 

Could not instantiate bean class [org.abc.def.XYZ]: No default constructor found;nested exception is java.lang.NoSuchMethodException: org.abc.def.XYZ./<init/>() 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681) 

我该怎么做才能解决这个问题?我无法将默认构造函数添加到XYZ。

我在我的调度程序servlet中添加了以下内容,但它仍然无效。

<bean name="token" class="org.abs.def.Xyx"> 
    <constructor-arg name="arg1" value="val1"/> 
    <constructor-arg name="arg2" value="val2"/> 
    <constructor-arg name="arg3" value="val3"/> 
</bean> 

谢谢。

回答

3

你可以在XML文件中将它定义为一个spring bean,传递所有必要的参数来实例化它。

样本:

<bean id="xyz" class="com.a.b.Xyz" > 
    <constructor-arg index="0" ref="anotherBean"/> 
    <constructor-arg index="1" value="12"/> 
</bean> 
+0

我试过这样做,但它不起作用。它抛出相同的错误! – shailesh

+0

你在哪里以及如何添加它?你如何初始化你的春天的上下文?你使用的是什么版本的春天? – fmucar

0

你需要提供<constructor-arg>元素在你的应用程序上下文的配置文件,如the documentation描述。

相关问题