简体   繁体   English

应用程序上下文中的Java Bean定义(春季)

[英]Java bean definition in application context (Spring)

I am new to Spring IOC, how can I convert this method to a bean definition in application context xml? 我是Spring IOC的新手,如何在应用程序上下文xml中将该方法转换为bean定义?

import com.sun.jersey.api.client.Client;
import com.sun.jersey.client.apache.ApacheHttpClient;
import com.sun.jersey.client.apache.config.ApacheHttpClientConfig;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

public static Client getRestClient() {
    // configuration for jersey client
    ApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
    Map<String, Object> properties = config.getProperties();
    properties.put(ApacheHttpClientConfig.PROPERTY_CONNECT_TIMEOUT,
            RESTUtil.dispatcherHttpTimeOut);

    // create client
    return ApacheHttpClient.create(config);
}

More detail: I want to get an instance of Client from the spring IOC, currently I use this method (getRestClient) to get it, so something like this: 更详细的信息:我想从Spring IOC获取Client的实例,当前我使用此方法(getRestClient)来获取它,因此如下所示:

<!-- Apache http rest client -->
<bean id="apacheHttpClient" name="Rest Client"
    class="com.sun.jersey.client.apache.ApacheHttpClient" factory-method="create">
    <constructor-arg></constructor-arg>
</bean>

Please let me know if any more information is needed. 请让我知道是否需要更多信息。

<bean id="apacheHttpClient" class="com.sun.jersey.client.apache.ApacheHttpClient" 
         factory-method="getRestClient"/>

Seems like you pretty much had it. 好像您几乎拥有了它。 Was it not working? 它不起作用吗? You just then need to pass this bean as a ref as a property or constructor arg to any class that needs to use it. 然后,您只需要将此bean作为ref作为属性或构造函数arg传递给需要使用它的任何类。

I guess what you wanted to ask was how to tell Spring to create the bean using a static factory method. 我猜您想问的是如何告诉Spring使用静态工厂方法创建bean。

This thread might help. 该线程可能会有所帮助。

<bean id="restClient" class="com.your.app.ClassWithTheFactoryMethod" factory-method="getRestClient"/> </bean>

should work 应该管用

This is the closest I could come to doing exactly what you had in your code. 这是我能来到这样你在你的代码有什么最接近的一次。 I had to refer to the ApacheHttpClientConfig.PROPERTY_CONNECT_TIMEOUT's value literally, and just put in 120 for RESTUtil.dispatcherHttpTimeOut (because I don't know what it is). 我必须从字面上引用ApacheHttpClientConfig.PROPERTY_CONNECT_TIMEOUT的值,并且仅将RESTUtil.dispatcherHttpTimeOut放入120(因为我不知道它是什么)。 Note that the "#{120}" expression is needed to pass that value in as an Integer rather than a String, which would cause an exception. 请注意,需要使用“#{120}”表达式将其值作为整数而不是字符串传递,这将导致异常。

<!-- Apache http rest client -->
<bean id="apacheHttpClient" name="Rest Client"
    class="com.sun.jersey.client.apache.ApacheHttpClient" factory-method="create">
    <constructor-arg>
        <bean class="com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig">
            <property name="properties['com.sun.jersey.client.property.connectTimeout']" value="#{120}" />
        </bean>
    </constructor-arg>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM