Using MethodInvokingFactoryBean to configure unconventional beans

I am trying to configure a HostConfiguration bean. One of the properties that it has is called proxyHost. However, the Apache HostConfiguration class does not comply with the java beans convention. setter for proxyHost takes an argument of type ProxyHost whereas getter returns a string.

I have the following snippet in applicationContext.xml.

    <bean id="proxyHost" class="org.apache.commons.httpclient.ProxyHost">
        <constructor-arg index="0" type="java.lang.String" value="myproxy.com" />
        <constructor-arg index="1" type="int" value="8087" />
    </bean>
    <bean id="hostConfiguration" class="org.apache.commons.httpclient.HostConfiguration">
         <property name="proxyHost" ref="proxyHost" />
    </bean>

When I try to load applicationContext for the application, I get the following error, since it HostConfigurationClassdoes not have getProxyHostwhich returns a ProxyHost or a setter that accepts a string: -

org.springframework.beans.NotWritablePropertyExcep: "proxyHost" bean class [org.apache.commons.httpclient.HostConfiguration]: bean "proxyHost" : ?

springsource thread, MethodInvokingFactoryBean, .

, MethodInvokingFactoryBean, proxyHost getProxyHost(), , ? , . , MethodInvokingFactoryBean. , - , MethodInvokingFactoryBean, .

beans HostConfiguration, spring?

!

+5
2

ProxyHost
(.. ProxyHost proxyHost = new ProxyHost("myproxy1.com",8080);

<bean id="proxyHost" class="org.apache.commons.httpclient.ProxyHost">
        <constructor-arg index="0" type="java.lang.String" value="myproxy1.com" />
        <constructor-arg index="1" type="int" value="8088" />
</bean>

HostConfiguration (.. HostConfiguration hostConfiguration = new HostConfiguration();

<bean id="hostConfiguration" class="org.apache.commons.httpclient.HostConfiguration" />

MethodInvokingFactoryBean setProxyHost() HostConfiguration ProxyHost .
(.. hostConfiguration.setProxyHost(proxyHost);)

 <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="targetObject">
                <ref local="hostConfiguration"/>
            </property>
            <property name="targetMethod">
                <value>setProxyHost</value>
            </property>
            <property name="arguments">
                <ref local="proxyHost"/>
            </property>
    </bean>
+9

, FactoryBean. spring 3.0, Java - @Configuration/@ Bean.

+1

All Articles