java 使用 MethodInvokingFactoryBean 设置非常规 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5400550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:56:36  来源:igfitidea点击:

Using MethodInvokingFactoryBean to set up unconventional beans

javaspring

提问by CoolBeans

I am trying to set up HostConfigurationbean. One of the property it has is called proxyHost. However, the apache HostConfiguration class does not follow the java beans convention. The setterfor the proxyHost accepts an argument of type ProxyHostwhereas the getterreturns a String.

我正在尝试设置HostConfigurationbean。它拥有的属性之一称为proxyHost。但是,apache HostConfiguration 类不遵循java bean 约定。该设定器用于接受的ProxyHost类型的参数的ProxyHost吸气剂返回一个字符串。

I have the following snippet in my applicationContext.xml.

我的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 HostConfigurationClassdoes not have a getProxyHostthat returns a ProxyHost or a setter that takes a String:-

当我尝试为应用程序加载 applicationContext 时,我收到以下错误,因为HostConfigurationClass没有getProxyHost返回 ProxyHost 或采用字符串的 setter:-

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?

org.springframework.beans.NotWritablePropertyException:bean 类 [org.apache.commons.httpclient.HostConfiguration] 的无效属性“proxyHost”:Bean 属性“proxyHost”不可写或具有无效的 setter 方法: setter 匹配 getter 的返回类型?

While searching on the springsource forum I came across this threadwhere it was recommended to use MethodInvokingFactoryBeanto solve this.

在 springsource 论坛上搜索时,我遇到了这个线程,建议使用MethodInvokingFactoryBean来解决这个问题。

I am not exactly sure how using MethodInvokingFactoryBeanwould help since I would need a return type of ProxyHostfrom 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 MethodInvokingFactoryBeanthat would be of immense help.

我不完全知道如何使用MethodInvokingFactoryBean将帮助,因为我需要的返回类型ProxyHost从方法getProxyHost()来解决这个问题,对不对?我也不确定如何在这种情况下使用它。我不清楚MethodInvokingFactoryBean. 因此,如果有人可以在上述上下文中给我一个如何使用的示例,MethodInvokingFactoryBean那将有很大帮助。

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

这也是普遍接受的设置 bean 的方式HostConfiguration,不遵循春季的惯例吗?

Thanks!

谢谢!

回答by Ken Chan

First , instantiate the ProxyHost
(i.e. ProxyHost proxyHost = new ProxyHost("myproxy1.com",8080);

首先,实例化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>

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

然后实例化HostConfiguration对象
(即HostConfiguration hostConfiguration = new HostConfiguration();

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

After that , use the MethodInvokingFactoryBeanto call setProxyHost()on the HostConfigurationand pass the proxyHostas argument.
(i.e. hostConfiguration.setProxyHost(proxyHost);)

在此之后,使用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>

回答by gkamal

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.

正如另一个答案中提到的,您可以实现FactoryBean。如果您使用的是 spring 3.0,您还可以查看Java 配置- @Configuration / @Bean。