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的模板方法模式,将这些重复的代码进 ...
随机推荐
- 一次显式GC导致的High CPU问题处理过程(转)
项目现场反馈系统出现性能问题,具体表现为:所有的客户端响应极其卡顿. 第一反应推测,难道是DB层面出现阻塞?检查v$session会话状态及等待类型未见异常,应该可以排除DB层面原因导致的可能. 继续 ...
- 免费iOS第三方推送工具Urban Airship使用教程
本文转载至 http://blog.csdn.net/mamong/article/details/8542404 http://www.dapps.net/dev/iphone/ios-free ...
- centos 6.5 安装图形界面【转】
最近想在centos 6.5上安装图形界面,在网上找到了方法.[原文链接] CentOS6相对于CentOS5的安装有了不少的进步,有不少默认的选项可以选择,如: Desktop :基本的桌面系统,包 ...
- [原创]adb使用教程v1.1.0-----by-----使用logcat快速抓取android崩溃日志
原文再续,书接上回:<使用logcat快速抓取android崩溃日志>中提到的工具包可以下载拉~ <使用logcat快速抓取android崩溃日志>:http://www.cn ...
- 同时调整lv分区的大小(减少一个,增加另一个)
author:headsen chen date: 2018-04-20 16:48:06 1.查看分区:/home 为67G,太大了,/ 是50g,太小了. [root@localhost ~]# ...
- Servlet监听器与Timer定时器配合实现JAVA WEB应用简单自动作业
在web应用中,有时候客户需要一些定时程序,不需要客户自己去操作,而是由应用程序自行触发执行某些操作.这个时候监听与定时器的配合使用就基本可以实现这个需求了. 1.创建一个监听的SERVELET,这个 ...
- 在java中public void与public static void有什么区别 ?
public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...
- Go & SQLite on Windows
一般golang使用的sqlite驱动包都是github.com/mattn/go-sqlite3,但是官方并没有直接支持windows平台的编译,因为windows平台编译默认需要gcc支持 其实解 ...
- 巨蟒python全栈开发flask4
1.偏函数 2.ThreadingLocal线程安全 空间换取时间 3.LocalStack 4.RunFlask+request 5.请求上文 6.请求下文
- Android主页导航:fragment+viewpager
简单实现Fragment+ViewPager实现主页导航控制,效果如下: 一.activity_main.xml布局文件: <?xml version="1.0" encod ...