我在模型中使用第三方库类 XYZ 作为参数。 XYZ 没有默认构造函数。所以 spring 无法为其创建 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>
Thanks.
您可以在 XML 文件中将其定义为 spring bean,并传递所有必要的参数来实例化它。
sample:
<bean id="xyz" class="com.a.b.Xyz" >
<constructor-arg index="0" ref="anotherBean"/>
<constructor-arg index="1" value="12"/>
</bean>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)