Spring很多mamcached实现

时间:2014-01-17 15:23:22

标签: java spring memcached

我需要在我的应用程序中使用几个memcached服务器。

直到现在我只使用一种配置:

<bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean" scope="singleton">
    <property name="servers" value="${app.memcached.url}"/>
    <property name="protocol" value="BINARY"/>
    <property name="transcoder">
        <bean class="net.spy.memcached.transcoders.SerializingTranscoder">
            <property name="compressionThreshold" value="1024"/>
        </bean>
    </property>
    <property name="opTimeout" value="1000"/>
    <property name="timeoutExceptionThreshold" value="1998"/>
    <property name="locatorType" value="CONSISTENT"/>
    <property name="failureMode" value="Redistribute"/>
    <property name="useNagleAlgorithm" value="false"/>
</bean>

当我想使用两台服务器时,只需要添加:

<bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean" scope="singleton">
    <property name="servers" value="${app.memcached.url}"/>
    <property name="protocol" value="BINARY"/>
    <property name="transcoder">
        <bean class="net.spy.memcached.transcoders.SerializingTranscoder">
            <property name="compressionThreshold" value="1024"/>
        </bean>
    </property>
    <property name="opTimeout" value="1000"/>
    <property name="timeoutExceptionThreshold" value="1998"/>
    <property name="locatorType" value="CONSISTENT"/>
    <property name="failureMode" value="Redistribute"/>
    <property name="useNagleAlgorithm" value="false"/>
</bean>

<bean id="memcachedAs" class="net.spy.memcached.spring.MemcachedClientFactoryBean" scope="singleton">
    <property name="servers" value="${app.memcached.url.as}"/>
    <property name="protocol" value="BINARY"/>
    <property name="transcoder">
        <bean class="net.spy.memcached.transcoders.SerializingTranscoder">
            <property name="compressionThreshold" value="1024"/>
        </bean>
    </property>
    <property name="opTimeout" value="1000"/>
    <property name="timeoutExceptionThreshold" value="1998"/>
    <property name="locatorType" value="CONSISTENT"/>
    <property name="failureMode" value="Redistribute"/>
    <property name="useNagleAlgorithm" value="false"/>
</bean>

然后我得到错误:

is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [net.spy.memcached.MemcachedClient] is defined: expected single matching bean but found 

您能帮助我,如何实施多种配置?

1 个答案:

答案 0 :(得分:1)

我假设您有一个像

这样的注射目标
@Autowired
private MemcachedClient client;

Spring会尝试按类型解析bean。但是你的上下文中有两个这种类型的bean,所以Spring不知道选择哪一个。相反,您可以通过ID

注入bean
@Resource(name="memcachedAs")
private MemcachedClient client;

甚至

@Autowired 
@Qualifier("memcachedAs")
private MemcachedClient client;