【问题标题】:MultiThreadedHttpConnectionManager can not Inject into serviceMultiThreadedHttpConnectionManager 无法注入服务
【发布时间】:2012-07-20 17:13:19
【问题描述】:

我现在正在使用resteasy 来构建一个rest 服务,但是使用默认的connectionManager(SimpleHttpConnectionManager),我只能在长期的事务方法中维持一个连接。所以我尝试用 MultiThreadedHttpConnectionManager 初始化 bean

我的 pom:

<properties>
    <resteasy.version>2.1.0.GA</resteasy.version>
    <spring.version>3.0.5.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.5.RELEASE</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
        <scope>provided</scope>
    </dependency>
<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-spring</artifactId>
        <version>${resteasy.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>${resteasy.version}</version>
        <scope>provided</scope>
    </dependency>

我的 applicationContext.xml

<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <constructor-arg>
        <bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"
            p:maxConnectionsPerHost="10"
            p:maxTotalConnections="20"/>
    </constructor-arg>
</bean>
<bean id="myRestService"
    class="org.jboss.resteasy.client.spring.RestClientProxyFactoryBean"
    p:serviceInterface="com.me.service.myRestService"
    p:baseUri="${serviceNameUrl}" 
    p:httpClient-ref="restHttpClient"/>

但是当我启动我的 jboss 6 服务器时,我得到了以下异常:

09:37:44,774 ERROR [ContextLoader] Context initialization failed: org.springframework.beans.factory.BeanCreationException:nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.me.service.myRestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Related cause: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myRestService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 
Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: 
Failed to convert property value of type 'org.apache.commons.httpclient.HttpClient' to required type 'org.apache.commons.httpclient.HttpClient' for property 'httpClient'; 
nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.commons.httpclient.HttpClient] to required type [org.apache.commons.httpclient.HttpClient] for property 'httpClient': 
no matching editors or conversion strategy found    

我尝试搜索resteasy包的依赖版本。但是所需的 commons-httpclient 是相同的(3.1 版)。那么为什么“HttpClient”不能转换为“HttpClient”呢?

【问题讨论】:

    标签: spring httpclient resteasy


    【解决方案1】:

    终于搞定了,问题是我添加了一个 3rd-party 依赖:

        <dependency>
         <groupId>net.java.dev.jets3t</groupId>
         <artifactId>jets3t</artifactId>
          <version>0.8.0</version>
    </dependency>
    

    但我没有具体说明:

        <scope>provided</scope>
    

    这将导致commons-httpclient-3.1.jar 将被编译到最终的war 文件中。

    同时,我在 {jboss_home}/server/default/lib 目录下添加了 commons-httpclient-3.1.jar。所以 jboss classloader 会在启动时加载 httpclient,而在加载这些 bean 时,会从包中加载 httpclient,这会导致上面的异常。

    因此,请始终注意那些 3rd-party 依赖项,我认为 90% 的无法从相同对象转换为相同对象异常是由这个重复加载器问题引起的。

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 2014-07-01
      • 2016-11-17
      • 1970-01-01
      • 2013-12-17
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多