应用程序上下文中的JavaBean定义(Spring)
我是SpringIOC的新手,如何将此方法转换为应用程序上下文xml中的bean定义应用程序上下文中的JavaBean定义(Spring),java,spring,Java,Spring,我是SpringIOC的新手,如何将此方法转换为应用程序上下文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.De
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);
}
导入com.sun.jersey.api.client.client;
导入com.sun.jersey.client.apache.ApacheHttpClient;
导入com.sun.jersey.client.apache.config.ApacheHttpClientConfig;
导入com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
公共静态客户端getRestClient(){
//jersey客户端的配置
ApacheHttpClientConfig=new DefaultApacheHttpClientConfig();
Map properties=config.getProperties();
properties.put(ApacheHttpClientConfig.PROPERTY\u CONNECT\u超时,
RESTUtil.dispatcherHttpTimeOut);
//创建客户端
返回ApacheHttpClient.create(config);
}
更多细节:我想从spring IOC获取一个客户端实例,目前我使用这个方法(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作为属性或构造函数arg传递给需要使用它的任何类。
看起来你差不多已经受够了。它不起作用了吗?然后,您需要将这个bean作为ref作为属性或构造函数arg传递给需要使用它的任何类。我想您想问的是如何告诉Spring使用静态工厂方法创建bean 也许会有帮助
应该可以我想您想问的是如何告诉Spring使用静态工厂方法创建bean 也许会有帮助
应该可以工作这是我能做的最接近你在代码中所做的事情了。我必须逐字地引用ApacheHttpClientConfig.PROPERTY\u CONNECT\u TIMEOUT的值,并为RESTUtil.dispatcherHttpTimeOut输入120(因为我不知道它是什么)。请注意,需要使用“#{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>
这是我能做的最接近于您在代码中所做的事情。我必须逐字地引用ApacheHttpClientConfig.PROPERTY\u CONNECT\u TIMEOUT的值,并为RESTUtil.dispatcherHttpTimeOut输入120(因为我不知道它是什么)。请注意,需要使用“#{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>
不确定“将方法转换为bean定义”是什么意思。您到底想做什么?不确定“将方法转换为bean定义”是什么意思。你到底想做什么?