How to use Spring to inject an object in a setter that doesnt follow the Java bean specification?

喜你入骨 提交于 2019-12-12 09:37:41

问题


I am trying to use Spring and wx-xmlrpc together. The problem is that XmlRpcClient has a setConfig() method that doesnt follow the Java Bean spec : the setter and the getter dont use the same Class. So Spring complaints when I have the following context.xml :

<bean id="xmlRpcClient" class="org.apache.xmlrpc.client.XmlRpcClient">
    <property name="config">
        <bean class="org.apache.xmlrpc.client.XmlRpcClientConfigImpl">
            <property name="serverURL" value="http://example.net" />
        </bean>
    </property>
</bean>

It says : Bean property 'config' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Is there a way to override that ? I know I could write a specific factory for this bean, but it seems to me that it is not the last time I find this kind of problem. I work a lot with legacy code of dubious quality ... Being able to use Spring XML configuration with it would be a great help !


回答1:


Write a FactoryBean for that class and have it call the correct setter.




回答2:


I don't think there is any way to override this using the Spring XML configuration.

An alternative to using a factory could be to make a subclass of XmlRpcClient that has a matching getter and setter for XmlRpcClientConfig (e.g. setClientConfig/getClientConfig). setClientConfig would simply call super.setConfig.



来源:https://stackoverflow.com/questions/1215048/how-to-use-spring-to-inject-an-object-in-a-setter-that-doesnt-follow-the-java-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!