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的模板方法模式,将这些重复的代码进 ...
随机推荐
- UIActionSheet样式问题心得
下午在做一个iPad的项目,需要用到一个 UIActionSheet. 点击popView中的“sort”按钮,触发出一个ActionSheet. self.action = [[[UIActionS ...
- hdu5861(Road)
题目链接:传送门 题目大意:有n个点 组成n-1段,每一段开着的时候都有花费Vi,有m组要求,对于每组要求 [x,y]之间可达,对于每一段你有一次开关的机会(最初都是关闭的) 问怎样安排段落得开闭时间 ...
- Entity Framework查询生成大量的子查询,如何避免?求救
最近使用Entity Framework做一个中型的项目,一张表含有千万条数据,并没有使用很复杂的查询,只是程序上使用了DTO进行帅选数据,且使用了分页,效果很不理想.经过跟踪sql,我发现很多简单的 ...
- 不固定宽度的div居中显示
对于div的居中 ,如果是有固定宽高的,可以加margin:auto;水平垂直居中,但如果是不固定宽高,又想让div居中的话,这种方式都可能不奏效,达不到想要的效果. 有两种方法:1.加display ...
- The Ultimate Guide To A/B Testing
w http://blog.jobbole.com/25576/?utm_source=blog.jobbole.com&utm_medium=relatedPosts https://www ...
- access variables from the global scope 在全局范围内访问变量的2种方法
w http://php.net/manual/zh/language.variables.scope.php http://php.net/manual/en/language.variables. ...
- vue-router 中 meta的用法
vue-router中的meta,也就是类似于面包屑的功能 路由 代码 用这个获取 嗯,就酱~~ 参考链接:https://blog.csdn.net/qq_32963841/article/deta ...
- 我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:
我的Android进阶之旅-->如何解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...
- Linux ssh面密码登录
1.生成自己的公钥和私钥 ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa 进入~/.ssh目录看多了两个文件:id_rsa id_rsa.pub 其中一个是公钥一 ...
- DateTimeTypeHandler
mysql-timestimp-------model-DateTime(jodatime) public class DataTimeTypeHandler extends BaseTypeHand ...