SSM+Redis整合

SSM+Redis整合

在搭建好SSM环境的基础上,整合Redis,在网上看了很多例子,记下来加深印象并做以后阅读。

1 创建一个Spring-redis.xml配置文件


<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"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
     <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>  
     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="${redis.maxIdle}" />  
        <property name="maxTotal" value="${redis.maxTotal}" />  
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />  
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
     bean>  

    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}">property>
        <property name="port" value="${redis.port}">property>
        <property name="password" value="${redis.password}">property>
       
         <constructor-arg index="0" ref="jedisPoolConfig" />    
    bean>
      
    
        
beans>

2 将spring-redis.xml里面需要的redis连接信息存到redis.properties中

redis.maxIdle=300
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=***//redis服务器所在IP地址
redis.port=6379
redis.password=***//登录redis的密码

3 将spring-redis.xml配置加入到web.xml中


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Applicationdisplay-name>
    <context-param>
        
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-redis.xml,classpath:spring-mybatis.xmlparam-value>
    context-param>
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <async-supported>trueasync-supported>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <listener>
        
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>

    
    <servlet-mapping>
    
        <servlet-name>defaultservlet-name> 
        <url-pattern>*.cssurl-pattern>
        <url-pattern>*.gifurl-pattern>
        <url-pattern>*.jpgurl-pattern>
        <url-pattern>*.jsurl-pattern>
        <url-pattern>*.pngurl-pattern>
        <url-pattern>*.mapurl-pattern>  
    servlet-mapping>



    

    <servlet>

        <servlet-name>Dispatcherservlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

        

        <init-param>

            <param-name>contextConfigLocationparam-name>

            <param-value>classpath:spring-mvc.xmlparam-value>

        init-param>

        <load-on-startup>1load-on-startup>

    servlet>

    

    <servlet-mapping>

        <servlet-name>Dispatcherservlet-name>

        <url-pattern>/url-pattern>

    servlet-mapping>

    <session-config>
        <session-timeout>120session-timeout>
    session-config>
    <mime-mapping>
        <extension>*.pptextension>
        <mime-type>application/mspowerpointmime-type>
    mime-mapping>

    <welcome-file-list>
        <welcome-file>/jsp/manage/ruleList.jspwelcome-file>
    welcome-file-list>
web-app>

4 用pom.xml下载相应的jar包,下载的spring-data-redis,jedis要配套,不然会出错

        <dependency>
            <groupId>org.springframework.datagroupId>
            <artifactId>spring-data-redisartifactId>
            <version>1.8.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
        dependency>

5 在我的ServerImpl里面,用下面的方式获取JedisConnection实例

 public JedisConnection getJedisConnectionFactory(){
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-redis.xml");  
            JedisConnectionFactory pool = (JedisConnectionFactory) context.getBean("jedisConnectionFactory");  
            JedisConnection jedis = (JedisConnection) pool.getConnection();
            return jedis;
        }

6 获取JedisConnection实例以后可用它的方法去测试是否联通Redis。

你可能感兴趣的:(SSM+Redis整合)