应用程序上下文中的 Java bean 定义(Spring)

标签 java spring

我是 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/

相关文章:

java - 在路径中包含 Java 7 的服务器上使用 Play Framework 2.4+

java - request.setCharacterEncoding ("UTF-8") 从 JBuilder2008 转到 myEclipse 时出错

SpringXD - Gemfire 作为接收器时出错

java - 如何在不同的端口上运行@RestController?

java - 在 java 项目中将方法(非 get 和 set)放在哪里才能符合 Spring MVC

java - Couchbase & Spring 查询错误 : "Query returning a primitive type are expected to return exactly 1 result, got X"

java - PaintComponent 与之前的方法交错

用于简单模式的 Java 正则表达式

java - 在没有互联网的情况下,需要更多时间从类路径加载 spring-*.xsd 文件

java - 从自定义注释中读取属性