bean class instantiation in spring for a class without default constructor

前端 未结 2 1578
日久生厌
日久生厌 2021-01-26 14:36

I am using a third party library class XYZ as an argument in my model. XYZ does not have a default constructor. So spring is not able to create bean for it giving error message

相关标签:
2条回答
  • 2021-01-26 15:24

    You can define it in the XML file as a spring bean passing all necessary parameters to instantiate it.

    sample:

    <bean id="xyz" class="com.a.b.Xyz" >
        <constructor-arg index="0" ref="anotherBean"/>
        <constructor-arg index="1" value="12"/> 
    </bean>
    
    0 讨论(0)
  • 2021-01-26 15:33

    You'll need to provide <constructor-arg> elements in your application context config file, as described in the documentation.

    0 讨论(0)
提交回复
热议问题