`
mengshijian4810
  • 浏览: 2176 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Spring+memcached整合

 
阅读更多

1.安装memcache

1)  下载memcached服务端memcached-1.2.6-win32-bin.zip,地址:http://code.jellycan.com/memcached/
2)  下载java版客户端 java_memcached-release_2.6.1.zip
3)  解压缩memcached-1.2.6-win32-bin.zip到指定目录,例如:D:\memcached-1.2.6-win32 ,
在终端(即cmd命令行界面),执行'D:\memcached-1.2.6-win32\memcached.exe -d install'
安装,再执行:'D:\memcached\memcached.exe -d start'启动,这样memcache就会作为windows系统服务在每 次开机时启动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调用的实现

 

分享到:
评论

相关推荐

    spring+memcached.jar

    标题中的"spring+memcached.jar"表明这是一个整合了Spring框架与Memcached缓存服务的Java应用程序。Memcached是一种广泛使用的分布式内存对象缓存系统,它能够通过减轻数据库负载来提高Web应用的性能。Spring框架是...

    springMVC+memcached整合

    此工程为 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聚合工程架构

    此套架构整合了springboot+memcached+mybatis+shiro+webservice的聚合式架构,内有具体代码,望大家一起学习交流,写博客因为太懒不愿意写 直接传了。请把解压后的java_memcached-release_2.6.3.jar和commons-pool-...

    springMVC+mybatis+jackson+memcached

    本示例项目"springMVC+mybatis+jackson+memcached"展示了如何整合四个关键的技术组件,来创建一个强大的后端架构。以下是对这些技术及其整合方式的详细说明: 1. **Spring MVC**:Spring MVC是Spring框架的一部分,...

    Nginx+Tomcat+Memcached实现tomcat集群和session共享 tomcat部分

    Nginx+Tomcat+Memcached实现tomcat集群和session共享 tomcat部分

    hibernate4+spring4+memcached 工程

    从【压缩包子文件的文件名称列表】"hibernate-memcached-master"来看,这可能是项目的根目录,暗示着项目中可能包含了一套完整的配置和实现,用于将Hibernate与Memcached整合。具体可能包括以下部分: 1. **配置...

    Spring与memcached整合

    标题“Spring与memcached整合”涉及的是在Java开发环境中如何将Spring框架与memcached缓存系统相结合使用,以便提升应用程序的性能和响应速度。这里我们将深入探讨这一整合过程中的关键概念和技术。 首先,...

    SpringMVC和memcached整合

    【SpringMVC与Memcached整合】是Java开发中常见的优化手段,主要目的是提高应用程序的响应速度,通过将频繁访问的数据存储在内存缓存系统中,减少对数据库的直接访问。以下将详细介绍整合过程中的关键步骤和涉及的...

    (SSM框架)memcached整合Spring基于Cache注解.

    在SSM框架中引入Memcached并基于Spring的Cache注解进行整合,可以实现高效、分布式的数据缓存,提升系统性能。下面将详细阐述这一过程中的关键知识点。 1. **Memcached介绍**: Memcached是一款高性能、分布式的...

    Spring整合memcached完整项目代码

    本文将深入探讨如何将Spring与Memcached整合,以及如何使用Spring Cache注解实现缓存功能。 首先,我们需要理解Spring Cache的基本概念。Spring Cache是一个抽象层,它允许我们在不依赖特定缓存实现的情况下使用...

    spring整合memcached

    这篇文章将详细介绍如何将Spring与Memcached整合,帮助新手入门这一技术栈。 首先,我们需要理解Spring整合Memcached的目的。在高并发的Web应用中,缓存可以显著提升性能,减少数据库的负载。Memcached因其简单高效...

    tomcat8 + nginx + memcached + cas 实现负载均衡的配置包

    &lt;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-mybatis-memcached.zip_Memcached java_annotation_mybatis_m

    ssm3-mybatis2-memcached 使用了 simple-...ssm3-springcache-mybatis3-memcached 通过 Spring Cache(Spring 3.1+) 实现 simple-spring-memcached 和 MyBatis3 整合。 simple-spring-memcached 使用了 JSON 序列化。

    java Memcached Spring整合代码实例

    java Memcached客户端代码,整合到Spring,提供Util工具类 代码片段: public class MemcachedUtils { private static boolean setExp(String key, Object value, Date expire) { boolean flag = false; try { ...

    SSM与memcached整合项目Spring Cache

    不过,具体这些文件如何与SSM(Spring、SpringMVC、MyBatis)和memcached整合的细节,需要参考实际的项目代码。 通过这样的整合,我们可以充分利用Spring Cache的便捷性和memcached的高性能,为应用程序构建一个...

    SSM框架Spring memcached整合基于注解形式

    在这个项目中,我们将探讨如何将Spring与memcached缓存系统进行整合,以提高应用程序的性能和响应速度。Memcached是一款高性能、分布式内存对象缓存系统,用于临时存储中间结果或频繁访问的数据,从而减轻数据库的...

    jar包整合:Springmvc+hibernate+Ehcache+shior+mysql+Oracle+fastjson

    1. **SpringMVC**:Spring MVC是Spring框架的一部分,它是一个基于模型-视图-控制器(MVC)设计模式的Web应用框架。它提供了处理HTTP请求、数据绑定、验证和视图渲染等功能,使得开发者可以轻松地构建可维护和测试的...

    springMVC-Mybatis-memcached整合流程

    ### Spring MVC-MyBatis-Memcached 整合流程详解 #### 一、概述 本文档将详细介绍如何在Spring MVC框架下集成MyBatis与Memcached。通过此整合方案,可以实现更高效的缓存管理机制,提高应用性能,降低数据库访问...

    SSM与memcached整合项目

    SSM与Memcached整合是一个常见的Java Web开发技术组合,用于提升应用的性能和可扩展性。SSM是指Spring、Spring MVC和MyBatis三个框架的集成,而Memcached则是一种高性能、分布式内存对象缓存系统。这个项目是将这三...

    Ehcache+xmemcached+redis 整合spring注解demo

    本项目" Ehcache+xmemcached+redis 整合spring注解demo"旨在演示如何将这三种主流的缓存技术——Ehcache、xmemcached和Redis——与Spring框架的注解功能相结合,以实现高效、灵活的缓存管理。以下是对这些知识点的...

Global site tag (gtag.js) - Google Analytics