1.安装memcache
|
2. 新建配置文件(spring级别)
新建名为spring-memcache.xml的spring配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> <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"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> </bean> </beans>
3.Web.xml文件中配置新建的文件
<!-- 配置spring的监听器,加载Spring配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/applicationContext-common.xml,classpath:/spring/spring-memcache.xml</param-value> </context-param>
4. 修改spring配置文件
修改applicationContext-common.xml配置文件。
1).添加properties配置文件(memcache.properties)去配置memcache的属性。
<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>
2).添加bean去初始化我们自己的一个spring工具类,一会进行详细解释。
<bean id="springContextHolder" class="com.hxrainbow.crm.util.SpringContextHolder"/>
5. Memcache配置文件
memcache.properties文件内容如下:
mcache.server=127.0.0.1\:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
6. 获得spring容器的工具类
/** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. **/ public class SpringContextHolder implementsApplicationContextAware{ private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */ public voidsetApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext= applicationContext; } /** * 取得存储在静态变量中的ApplicationContext. */ public staticApplicationContext 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") 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 voidcheckApplicationContext() { if (applicationContext == null) { throw newIllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } } }
首先说一下ApplicationContextAware这个接口,这个接口中有一个方法:
void setApplicationContext(ApplicationContext applicationContext)
下面是这个方法的简单说明:
Set the ApplicationContext that this object runs in.Normally this call will be used to initialize the object.
我们在配置文件中配置了bean的初始化,然后他就可以用于获得spring容器中的东西了。
7.memcache的工具类
public class MemcacheUtil { public static MemCachedClient getMemCachedClient() { return SpringContextHolder.getBean("memcachedClient"); } } |
8.junit测试类
public class MemcacheUtilTest { static MemCachedClient memcachedClient; @BeforeClass public static void setUpBeforeClass() throws Exception { ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"/spring/applicationContext-common.xml","/spring/spring-memcache.xml"}); } @Test public void s() { MemCachedClient m=SpringContextHolder.getBean("memcachedClient"); m.set("name", "yunhui"); System.out.println(m.get("name")); } } |
9. Java对memcache调用的实现
相关推荐
此工程为 springMVC+memcached整合 项目。 resources\config\memcached.xml为 memcached配置文件。test\cn\sky\tian\test\spring为Demo的路径。 (里面有个src\cn\sky\tian\test\controller路径是MengoDB的demo,也...
此套架构整合了springboot+memcached+mybatis+shiro+webservice的聚合式架构,内有具体代码,望大家一起学习交流,写博客因为太懒不愿意写 直接传了。请把解压后的java_memcached-release_2.6.3.jar和commons-pool-...
Nginx+Tomcat+Memcached实现tomcat集群和session共享 tomcat部分
memcached+spring整合 memcached+spring整合
适合java程序员开发springMVC整合memcached参考
NULL 博文链接:https://rainyear.iteye.com/blog/1166036
<bean class="net.spy.memcached.spring.MemcachedClientFactoryBean" p:servers="127.0.0.1:11211" p:protocol="BINARY" p:locatorType="ARRAY_MOD" p:failureMode="Redistribute" p:transcoder-ref=...
使用Spring,SpringMVC,mybatis框架 将memcached和Spring集成,基于Spring原Cache注解
Spring整合memcached,基于Spring Cache注解!
ssm3-mybatis2-memcached 使用了 simple-...ssm3-springcache-mybatis3-memcached 通过 Spring Cache(Spring 3.1+) 实现 simple-spring-memcached 和 MyBatis3 整合。 simple-spring-memcached 使用了 JSON 序列化。
java Memcached客户端代码,整合到Spring,提供Util工具类 代码片段: public class MemcachedUtils { private static boolean setExp(String key, Object value, Date expire) { boolean flag = false; try { ...
spring整合memcached 新手入门
完整的Spring整合memcached项目,maven工程,下载即可用
内含Springmvc+hibernate+Ehcache+shior+mysql+Oracle+fastjson,jar包整合,自己的加个jdk1.7便可使用。
承接上个代码将memecached调用改为交给Spring Cache代理
mybatis-memcached-1.0.0.jar mybatis-spring-1.2.2.jar mybatis-3.2.7.jar spring-aop-4.0.6.RELEASE.jar 等等搭建框架所需要的jar包
ssh框架搭建教程 ssh框架整合步骤讲解 Struts+Spring+Hibernate整合教程 Struts2、Spring和Hibernate应用实例
本博客源码采用hibernate4.3.6+spring4.0.6+struts2.3.16整合开发,集成memcached。加入本地缓存。html5前端页面,清一色ajax加载,后端采用easyUi。后台功能有待完善。By:WebSos
包含最简单的memcache的使用,测试类MemCached.java; 在此基础上增加了memcahce和spring的整合,顺便使用了ibati,测试类SpringMemcacheTest.java 代码简洁,适合研究,参考
spring boot 实践学习案例,与其它组件结合如 mybatis、jpa、dubbo、redis、mongodb、memcached、kafka、rabbitmq、activemq、elasticsearch、security、shiro等 #### Spring Boot 版本 - 2.0.3.RELEASE #### 模块...