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的模板方法模式,将这些重复的代码进 ...
随机推荐
- Linux系统优化之网络IO调优
修改 vi /etc/sysctl.conf后执行命令 sysctl -p立即生效 首先,系统的不同也会导致 /etc/下的 文件的不同,原本powerpc 环境下 在 /etc/init.d/下有个 ...
- [MySQL] 变量(参数)的查看和设置 [转]
[MySQL] 变量(参数)的查看和设置 类似于Oracle的参数文件,MySQL的选项文件(如my.cnf)用于配置MySQL服务器,但和Oracle叫法不一样,在MySQL里, 官方叫变量(Var ...
- encodeURI() 的用法
定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码.[通用资源标识符(Uniform Resource Identifier, 简称"URI")] 语法 en ...
- 转SpringMVC Controller 返回值的可选类型
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...
- 【转】windows 下 goprotobuf 的安装与使用
1. 安装 在网上看了很多教程,都提到要安装 protoc 与 protoc-gen-go,但通过尝试之后并不能正确安装 protoc,一下记录能够顺利安装 protoc 与 protoc-gen-g ...
- linux虚拟机连不上网络
1.修改文件ifcfg-ens33 # 切换目录 [root@localhost ~]# cd /etc/sysconfig/network-scripts/ # 将ONBOOT修改为yes [roo ...
- Django框架错误处理
错误处理 在一些网站开发中.经常会需要捕获一些错误,然后将这些错误返回比较优美的界面,或者是将这个错误的请求做一些日志保存.那么我们本节就来讲讲如何实现. 常用的错误码: 404:服务器没有指定的ur ...
- 我的Android进阶之旅------>解决错误: java.util.regex.PatternSyntaxException: Incorrect Unicode property
1.错误描述 今天使用正则表达式验证密码的时候,报了错误 java.util.regex.PatternSyntaxException: Incorrect Unicode property near ...
- 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
今天使用第三方Jar包afinal.jar时候.想看一下源码,无法看 然后像加入jar相应的源代码包.也无法加入相应的源代码,报错例如以下:The current class path entry b ...
- jQuery.extend()、jQuery.fn.extend()扩展方法具体解释
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/dreamsunday/article/details/25193459 jQuery自己定义了jQu ...