1, 开始肯定是下载需要的文件了,这里就下载附件里的文件就好,我也是在网上down的,放这好找。然后我们安装一下Memcache服务器,找到解压的memcached-1.2.1-win32,启动cmd ,进入解压目录,输入命令 D:\memcached-1.2.6-win32\memcached.exe -d install.然后再键入命令'D:\memcached\memcached.exe -d start'启动,这样memcache就会作为windows系统服务在每次开机时启动memcache服务。

2,下面我们开始在使用java进行配置开发。添加Spring功能。在web.xml中添加配置。

 
  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:/spring/applicationContext-common.xml,
  4. classpath:/spring/spring-memcache.xml
  5. </param-value>
  6. </context-param>

3,在src下新建spring目录,并新建applicationContext-common.xml和spring-memcache.xml。内容分别如下。

applicationContext-common.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7. <bean
  8. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  9. <property name="locations">
  10. <list>
  11. <value>classpath:memcache.properties</value>
  12. <value>classpath:jdbc.properties</value>
  13. </list>
  14. </property>
  15. </bean>
  16. <bean id="springContextHolder" class="com.hxrainbow.crm.util.SpringContextHolder"/>
  17. </beans>

spring-memcache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7. <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
  8. init-method="initialize"    destroy-method="shutDown">
  9. <property name="servers">
  10. <list>
  11. <value>${memcache.server}</value>
  12. </list>
  13. </property>
  14. <property name="initConn">
  15. <value>${memcache.initConn}</value>
  16. </property>
  17. <property name="minConn">
  18. <value>${memcache.minConn}</value>
  19. </property>
  20. <property name="maxConn">
  21. <value>${memcache.maxConn}</value>
  22. </property>
  23. <property name="maintSleep">
  24. <value>${memcache.maintSleep}</value>
  25. </property>
  26. <property name="nagle">
  27. <value>${memcache.nagle}</value>
  28. </property>
  29. <property name="socketTO">
  30. <value>${memcache.socketTO}</value>
  31. </property>
  32. </bean>
  33. <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
  34. </bean>
  35. </beans>

在配置文件中我们会看到memcache.properties,jdbc.properties和springContextHolder。

他们的内容分别是:

  1. memcache.properties
  2. memcache.server=127.0.0.1:11211
  3. memcache.initConn=20
  4. memcache.minConn=10
  5. memcache.maxConn=50
  6. memcache.maintSleep=3000
  7. memcache.nagle=false
  8. memcache.socketTO=3000
  1. jdbc.properties
  1. import java.util.Map;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. /**
  5. *
  6. * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
  7. **/
  8. public class SpringContextHolder implements ApplicationContextAware {
  9. private static ApplicationContext applicationContext;
  10. /**
  11. *
  12. * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
  13. */
  14. public void setApplicationContext(ApplicationContext applicationContext) {
  15. SpringContextHolder.applicationContext = applicationContext;
  16. }
  17. /**
  18. *
  19. * 取得存储在静态变量中的ApplicationContext.
  20. */
  21. public static ApplicationContext getApplicationContext() {
  22. checkApplicationContext();
  23. return applicationContext;
  24. }
  25. /**
  26. *
  27. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
  28. */
  29. @SuppressWarnings("unchecked")
  30. public static <T> T getBean(String name) {
  31. checkApplicationContext();
  32. return (T) applicationContext.getBean(name);
  33. }
  34. /**
  35. *
  36. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
  37. *
  38. * 如果有多个Bean符合Class, 取出第一个.
  39. */
  40. @SuppressWarnings({ "unchecked", "rawtypes" })
  41. public static <T> T getBean(Class<T> clazz) {
  42. checkApplicationContext();
  43. Map beanMaps = applicationContext.getBeansOfType(clazz);
  44. if (beanMaps != null && !beanMaps.isEmpty()) {
  45. return (T) beanMaps.values().iterator().next();
  46. } else {
  47. return null;
  48. }
  49. }
  50. private static void checkApplicationContext() {
  51. if (applicationContext == null) {
  52. throw new IllegalStateException(
  53. "applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
  54. }
  55. }

配置文件我们写完了,下面我们开始测试使用。

做Bean类

  1. import java.io.Serializable;
  2. public class Bean implements Serializable{
  3. /**
  4. *
  5. */
  6. private static final long serialVersionUID = 1L;
  7. private String name;
  8. private int age;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public int getAge() {
  16. return age;
  17. }
  18. public void setAge(int age) {
  19. this.age = age;
  20. }
  21. public String toString() {
  22. String bean = "{name:"+this.getName()+",age:"+this.getAge()+"}";
  23. return bean;
  24. }
  25. }

做测试使用类:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;
  5. import com.danga.MemCached.MemCachedClient;
  6. import com.danga.MemCached.SockIOPool;
  7. public class MemcacheUtilTest {
  8. public static void main(String[] args) {
  9. ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"src/spring/spring-memcache.xml","src/spring/applicationContext-common.xml"});
  10. SockIOPool s =SpringContextHolder.getBean("memcachedPool");
  11. System.out.println("s="+s.getInitConn());
  12. MemCachedClient mc = (MemCachedClient) ctx.getBean("memcachedClient");
  13. //开始设值
  14. mc.set("name", " string  ");
  15. mc.set("int", 5);
  16. mc.set("double", 5.5);
  17. Bean bean = new Bean();
  18. bean.setAge(21);
  19. bean.setName("名字");
  20. mc.set("bean", bean);
  21. List<Bean> data = new ArrayList<Bean>();
  22. for(int i=0;i<3;i++)
  23. {
  24. Bean xbean = new Bean();
  25. xbean.setAge(i);
  26. xbean.setName("test_"+i);
  27. data.add(xbean) ;
  28. }
  29. mc.set("data", data);
  30. try{
  31. Thread.sleep(50);
  32. //开始取值
  33. String name =(String) mc.get("name");
  34. int i = (Integer) mc.get("int");
  35. double d = (Double) mc.get("double") ;
  36. Bean b = (Bean) mc.get("bean") ;
  37. data =  (List<Bean>) mc.get("data") ;
  38. System.out.println("字符串:"+name);
  39. System.out.println("数字型:"+i);
  40. System.out.println("双精度:"+d);
  41. System.out.println("bean  toString :"+b.toString());
  42. System.out.println("data  toString :"+data.toString());
  43. //开始删除值
  44. System.out.println("开始删除 :》》》》》》》》》");
  45. mc.delete("name");
  46. mc.delete("int");
  47. mc.delete("double");
  48. mc.delete("bean");
  49. String name_d =(String) mc.get("name");
  50. int i_d = (Integer) mc.get("int");
  51. double d_d = (Double) mc.get("double") ;
  52. Bean b_d = (Bean) mc.get("bean") ;
  53. System.out.println("字符串:"+name_d);
  54. System.out.println("数字型:"+i_d);
  55. System.out.println("双精度:"+d_d);
  56. System.out.println("bean  toString :"+b_d.toString());
  57. }catch(Exception e){
  58. e.printStackTrace();
  59. }
  60. }
  61. }

运行一下,看看结果吧。具体可参考java_memcached-release_1.6\doc\HOWTO.txt.注意Bean要实现序列化。

spring与memcached整合[转]的更多相关文章

  1. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  2. 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)

    硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...

  3. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)

    轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...

  4. Spring Security 3整合CAS 实现SSO

    spring security 3整合cas client用于实现各Application之间的单点登录. 1. 需要准备的jar spring-security-core-3.0.8.RELEASE ...

  5. 1.springMVC+spring+Mybatis的整合思路

    SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...

  6. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  7. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  8. Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  9. Mybatis学习--spring和Mybatis整合

    简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...

随机推荐

  1. AndroidManifest.xml文件详解(activity)(四)

    android:multiprocess 这个属性用于设置Activity的实例能否被加载到与启动它的那个组件所在的进程中,如果设置为true,则可以,否则不可以.默认值是false. 通常,一个新的 ...

  2. IOS控件:分歧解决其(UILabel 和 IBAction)

    #import <UIKit/UIKit.h> @interface demo7_dayViewController : UIViewController { // 用来显示程序结果 IB ...

  3. springboot2.1.1 中集成websocket 单元测试异常

    单元测试在没有集成websocket之前是好好的,当集成websocket之后就出现了下面的异常(只贴出来关键信息): 2019-01-11 10:05:42 [ERROR] [org.springf ...

  4. HDU 5892 Resident Evil

    题目链接:传送门 题目大意:有50种动物,给你n*n的矩阵,m次操作,P代表加入操作,在左上角 x1,y1 到右下角 x2,y2,的矩形范围内加入 种类为x,数量为y的动物. Q代表询问操作,在左上角 ...

  5. RPC远程过程调用概念及实现

    RPC框架学习笔记 >>什么是RPC RPC 的全称是 Remote Procedure Call 是一种进程间通信方式. 它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过 ...

  6. linux必学

    memcache zookeeper activemq

  7. 用jq实现鼠标移入按钮背景渐变其他的背景效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Web 编程中路径问题

    web.xml 中 <url-pattern> 路径(即 Servlet 路径) 要么以 "*" 开头, 要么以 "/" 开头. 转发和包含路径(服 ...

  9. xshell上传下载文件

    上传文件到服务器rz 从服务器下载文件sz

  10. Redis的LRU机制(转)

    原文:Redis的LRU机制 在Redis中,如果设置的maxmemory,那就要配置key的回收机制参数maxmemory-policy,默认volatile-lru,参阅Redis作者的原博客:a ...