XMemcached与Spring集成

1 xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mongo="http://www.springframework.org/schema/data/mongo"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

 
  

 
  <!-- 使用注解 -->
 <context:component-scan base-package="com.*">
 
 </context:component-scan>
 
<bean    id="memcachedClientBuilder"     class="net.rubyeye.xmemcached.XMemcachedClientBuilder" 
          p:connectionPoolSize="8"  
           p:failureMode="true"> 
 
        <constructor-arg> 
             <list> 
                 <bean class="java.net.InetSocketAddress"> 
                     <constructor-arg> 
                        <value>localhost</value> 
                    </constructor-arg> 
                     <constructor-arg> 
                        <value>11211</value> 
                     </constructor-arg> 
                </bean> 
                <bean class="java.net.InetSocketAddress"> 
                    <constructor-arg> 
                         <value>localhost</value> 
                    </constructor-arg> 
                     <constructor-arg> 
                         <value>11212</value> 
                    </constructor-arg> 
                </bean> 
            </list> 
         </constructor-arg> 
            <constructor-arg> 
             <list> 
                 <value>1</value> 
                 <value>1</value> 
             </list> 
         </constructor-arg> 


        <property name="commandFactory"> 
             <bean class="net.rubyeye.xmemcached.command.TextCommandFactory" /> 
         </property> 
         <property name="sessionLocator"> 
             <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" /> 
         </property> 
         <property name="transcoder"> 
             <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" /> 
         </property> 
     </bean> 
     <!-- Use factory bean to build memcached client --> 
     <bean 
         id="memcachedClient" 
           factory-bean="memcachedClientBuilder" 
           factory-method="build" 
           destroy-method="shutdown" /> 

 
</beans>

这里的memcachedClientBuilder节点完成MemcachedClientBuilder,然后通过memcachedClient节点配置factory-method,调用MemcachedClientBuilder的build()方法产生MemcachedClient,并配置destroy-method进行关闭。
有了Spring容器支持,我们不需要在代码中进行配置,也不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。

2 java 测试代码

 

 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");  
  MemcachedClient  memcachedClient= (MemcachedClient) app.getBean("memcachedClient");  
  memcachedClient.putObj("messi", "chease", 60*60);
  String value = memcachedClient.getObj("messi" ).toString();

 System. out .println( "value=" + value);

 

你可能感兴趣的:(memcached)