spring与memcached整合[转]
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中添加配置。
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/spring/applicationContext-common.xml,
- classpath:/spring/spring-memcache.xml
- </param-value>
- </context-param>
3,在src下新建spring目录,并新建applicationContext-common.xml和spring-memcache.xml。内容分别如下。
applicationContext-common.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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:memcache.properties</value>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <bean id="springContextHolder" class="com.hxrainbow.crm.util.SpringContextHolder"/>
- </beans>
spring-memcache.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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
- init-method="initialize" destroy-method="shutDown">
- <property name="servers">
- <list>
- <value>${memcache.server}</value>
- </list>
- </property>
- <property name="initConn">
- <value>${memcache.initConn}</value>
- </property>
- <property name="minConn">
- <value>${memcache.minConn}</value>
- </property>
- <property name="maxConn">
- <value>${memcache.maxConn}</value>
- </property>
- <property name="maintSleep">
- <value>${memcache.maintSleep}</value>
- </property>
- <property name="nagle">
- <value>${memcache.nagle}</value>
- </property>
- <property name="socketTO">
- <value>${memcache.socketTO}</value>
- </property>
- </bean>
- <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
- </bean>
- </beans>
在配置文件中我们会看到memcache.properties,jdbc.properties和springContextHolder。
他们的内容分别是:
- memcache.properties
- memcache.server=127.0.0.1:11211
- memcache.initConn=20
- memcache.minConn=10
- memcache.maxConn=50
- memcache.maintSleep=3000
- memcache.nagle=false
- memcache.socketTO=3000
- jdbc.properties
- import java.util.Map;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- /**
- *
- * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
- **/
- public class SpringContextHolder implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- /**
- *
- * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
- */
- public void setApplicationContext(ApplicationContext applicationContext) {
- SpringContextHolder.applicationContext = applicationContext;
- }
- /**
- *
- * 取得存储在静态变量中的ApplicationContext.
- */
- public static ApplicationContext getApplicationContext() {
- checkApplicationContext();
- return applicationContext;
- }
- /**
- *
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
- */
- @SuppressWarnings("unchecked")
- public static <T> T getBean(String name) {
- checkApplicationContext();
- return (T) applicationContext.getBean(name);
- }
- /**
- *
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
- *
- * 如果有多个Bean符合Class, 取出第一个.
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public static <T> T getBean(Class<T> clazz) {
- checkApplicationContext();
- Map beanMaps = applicationContext.getBeansOfType(clazz);
- if (beanMaps != null && !beanMaps.isEmpty()) {
- return (T) beanMaps.values().iterator().next();
- } else {
- return null;
- }
- }
- private static void checkApplicationContext() {
- if (applicationContext == null) {
- throw new IllegalStateException(
- "applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
- }
- }
配置文件我们写完了,下面我们开始测试使用。
做Bean类
- import java.io.Serializable;
- public class Bean implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String toString() {
- String bean = "{name:"+this.getName()+",age:"+this.getAge()+"}";
- return bean;
- }
- }
做测试使用类:
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import com.danga.MemCached.MemCachedClient;
- import com.danga.MemCached.SockIOPool;
- public class MemcacheUtilTest {
- public static void main(String[] args) {
- ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"src/spring/spring-memcache.xml","src/spring/applicationContext-common.xml"});
- SockIOPool s =SpringContextHolder.getBean("memcachedPool");
- System.out.println("s="+s.getInitConn());
- MemCachedClient mc = (MemCachedClient) ctx.getBean("memcachedClient");
- //开始设值
- mc.set("name", " string ");
- mc.set("int", 5);
- mc.set("double", 5.5);
- Bean bean = new Bean();
- bean.setAge(21);
- bean.setName("名字");
- mc.set("bean", bean);
- List<Bean> data = new ArrayList<Bean>();
- for(int i=0;i<3;i++)
- {
- Bean xbean = new Bean();
- xbean.setAge(i);
- xbean.setName("test_"+i);
- data.add(xbean) ;
- }
- mc.set("data", data);
- try{
- Thread.sleep(50);
- //开始取值
- String name =(String) mc.get("name");
- int i = (Integer) mc.get("int");
- double d = (Double) mc.get("double") ;
- Bean b = (Bean) mc.get("bean") ;
- data = (List<Bean>) mc.get("data") ;
- System.out.println("字符串:"+name);
- System.out.println("数字型:"+i);
- System.out.println("双精度:"+d);
- System.out.println("bean toString :"+b.toString());
- System.out.println("data toString :"+data.toString());
- //开始删除值
- System.out.println("开始删除 :》》》》》》》》》");
- mc.delete("name");
- mc.delete("int");
- mc.delete("double");
- mc.delete("bean");
- String name_d =(String) mc.get("name");
- int i_d = (Integer) mc.get("int");
- double d_d = (Double) mc.get("double") ;
- Bean b_d = (Bean) mc.get("bean") ;
- System.out.println("字符串:"+name_d);
- System.out.println("数字型:"+i_d);
- System.out.println("双精度:"+d_d);
- System.out.println("bean toString :"+b_d.toString());
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
运行一下,看看结果吧。具体可参考java_memcached-release_1.6\doc\HOWTO.txt.注意Bean要实现序列化。
spring与memcached整合[转]的更多相关文章
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)
Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)
硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- Spring Security 3整合CAS 实现SSO
spring security 3整合cas client用于实现各Application之间的单点登录. 1. 需要准备的jar spring-security-core-3.0.8.RELEASE ...
- 1.springMVC+spring+Mybatis的整合思路
SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
- Mybatis学习--spring和Mybatis整合
简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...
随机推荐
- Java Base64解析
最近在业务场景中,需要对第三方传递进来的字符进行base64解密,根据第三方文档提供的解析工具,对数据进行了解析,关于Base64的解析方式如下: String sign = "xxxxxx ...
- 【原】storm源码之巧用java反射反序列化clojure的defrecord获取属性值
storm源码是clojure.java.python的混合体.在解决storm-0.8.2的nimbus单点问题的过程中需要从zookeeper上读取目前storm集群中正在运行的assignmen ...
- git +vs2017 操作手册+目前工作流程图
分支名称介绍及命名规则: 测试分支:master 线上稳定分支:master-发布分支 功能分支:命名命名规则:V版本号-开发者姓名-功能名. 紧急修复分支:命名规则:fixbug-版本号-具体问题名 ...
- 在Editplus中Dev C++配置C++的编译运行环境
1.首先得下载安装DEV-cpp 2.打开Editplus编辑器,工具->配置自定义工具 3.具体配置 编译C:命令:D:\Dev-Cpp\MinGW64\bin\g++.exe参数:" ...
- Class.forName()用法
主要功能 Class.forName(xxx.xx.xx)返回的是一个类. Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段. ...
- ASP.NET Identity 2集成到MVC5项目--笔记02
ASP.NET Identity 2集成到MVC5项目--笔记01 ASP.NET Identity 2集成到MVC5项目--笔记02 继上一篇,本篇主要是实现邮件.用户名登陆和登陆前邮件认证. 1. ...
- 手机APP卸载原因 不会卸载
- windows python easy_install ,pip. selenium
http://www.cnblogs.com/fnng/p/3157639.html 搭建平台windows 准备工具如下: unknown encoding: cp65001异常 python安装后 ...
- java工程编写manifest文件
如果需要一个可以单独运行的jar包“Runnable JAR file”,省事的方法是妥妥的选择打一个可运行的jar包“Runnable JAR file”.如此一来,就可以把程序运行所依赖的类.第三 ...
- python引入同一目录下的py文件
python引入同一目录下的py文件 注意:python2和python3的包内import语法有区别,下面介绍一下python3的包内import语法 例如在admin.py文件中要引入dealco ...