我是 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);
}
更多细节:我想从 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>
如果需要更多信息,请告诉我。
最佳答案
<bean id="apacheHttpClient" class="com.sun.jersey.client.apache.ApacheHttpClient"
factory-method="getRestClient"/>
看来您已经很满意了。它不起作用吗?然后您需要将此 bean 作为属性或构造函数参数作为 ref 传递给需要使用它的任何类。
关于应用程序上下文中的 Java bean 定义(Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450098/