Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MethodInvokingFactoryBean to set up unconventional beans

Tags:

java

spring

I am trying to set up HostConfiguration bean. One of the property it has is called proxyHost. However, the apache HostConfiguration class does not follow the java beans convention. The setter for the proxyHost accepts an argument of type ProxyHost whereas the getter returns a String.

I have the following snippet in my 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 the applicationContext for the app I get the following error since HostConfigurationClass does not have a getProxyHost that returns a ProxyHost or a setter that takes a String:-

org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'proxyHost' of bean class [org.apache.commons.httpclient.HostConfiguration]: Bean property 'proxyHost' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?

While searching on the springsource forum I came across this thread where it was recommended to use MethodInvokingFactoryBean to solve this.

I am not exactly sure how using MethodInvokingFactoryBean would help since I would need a return type of ProxyHost from the method getProxyHost() to fix this, right? And I am not also sure about how to use it in this context. I am not clear on the internals of MethodInvokingFactoryBean. Therefore, if someone could please give me an example in the above context how to use MethodInvokingFactoryBean that would be of immense help.

Also is this generally the accepted way to set up beans like HostConfiguration that do not follow convention in spring?

Thanks!

like image 621
CoolBeans Avatar asked Mar 23 '11 03:03

CoolBeans


2 Answers

First , instantiate the ProxyHost
(i.e. 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>

Then instantiate the HostConfiguration object
(i.e. HostConfiguration hostConfiguration = new HostConfiguration();

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

After that , use the MethodInvokingFactoryBean to call setProxyHost() on the HostConfiguration and pass the proxyHost as argument.
(i.e. 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>
like image 161
Ken Chan Avatar answered Nov 13 '22 15:11

Ken Chan


As mentioned in the other answer you can implement a FactoryBean. If you are using spring 3.0 you could also take a look at the Java Configuration - @Configuration / @Bean.

like image 33
gkamal Avatar answered Nov 13 '22 16:11

gkamal