I am new to Spring IOC, how can I convert this method to a bean definition in application context xml?
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:
<!-- 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.
I guess what you wanted to ask was how to tell Spring to create the bean using a static factory method.
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). Note that the "#{120}" expression is needed to pass that value in as an Integer rather than a String, which would cause an exception.
<!-- 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.