zoukankan      html  css  js  c++  java
  • 函数集成redis与Spring集成

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正

        redis与Spring集成比较简单,本文不触及Spring的一些基本概念,读都需先具有Spring的相干知识。

        先在maven中添加依赖

        pom.xml

    <!-- redis -->
    		<dependency>
    			<groupId>redis.clients</groupId>
    			<artifactId>jedis</artifactId>
    			<version>2.0.0</version>
    		</dependency>
    		<!-- redis -->

        applicationContext-redis.xml加入你的Spring context

    <?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:mongo="http://www.springframework.org/schema/data/mongo"
    	xsi:schemaLocation="http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.springframework.org/schema/data/mongo
              http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    		<property name="maxActive" value="50" />
    		<property name="maxIdle" value="10" />
    		<property name="maxWait" value="1000" />
    		<property name="testOnBorrow" value="true" />
    	</bean>
    
    	<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
    		<constructor-arg index="0" value="172.21.1.121" />
    		<constructor-arg index="1" value="6379" />
    	</bean>
    
    	<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
    		<constructor-arg index="0" ref="jedisPoolConfig" />
    		<constructor-arg index="1">
    			<list>
    				<ref bean="jedis.shardInfo" />
    			</list>
    		</constructor-arg>
    	</bean>
    </beans>
        每日一道理
    因为自信,在呀呀学语时,我靠着纤嫩的双腿,迈出人生的第一步;因为自信,我一次次将第一名的奖状高高举起;因为自信,我毫不吝惜地剪掉飘逸的长发,在运动场上展现风采……感谢自信,它给了我一双翅膀,让我在电闪雷鸣中去飞翔,在风雨中去搏击人生!

        RedisDao.java操纵redis的类

    /**
     * 
     */
    package com.zolcorp.mahout.dao;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import redis.clients.jedis.ShardedJedis;
    import redis.clients.jedis.ShardedJedisPool;
    
    /**
     * @author hadoop
     *
     */
    public class RedisDao {
    	
    	@Autowired
    	private ShardedJedisPool shardedJedisPool;
    	
    	public void set(String key, String value)
    	{
    		ShardedJedis jedis =  shardedJedisPool.getResource();
    		jedis.set(key, value);
    	}
    	
    	public String get(String key)
    	{
    		ShardedJedis jedis =  shardedJedisPool.getResource();
    		return jedis.get(key);
    	}
    }

        其它一些方法,请读者自己完成。

    <bean id="redisDao" class="com.zolcorp.mahout.dao.RedisDao"></bean>

        注册bean

        main函数如下:

    /**
     * 
     */
    package com.zolcorp.mahout.main;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    import com.zolcorp.mahout.dao.RedisDao;
    
    /**
     * @author hadoop
     *
     */
    public class RedisMain {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		ApplicationContext context = new FileSystemXmlApplicationContext(
    				new String[] { "classpath:applicationContext-bean.xml",
    						"classpath:applicationContext-datasource.xml", "classpath:applicationContext-redis.xml"});
    		RedisDao redisDao = context.getBean("redisDao", RedisDao.class);
    		redisDao.set("test", "test-----ming...");
    		System.out.println(redisDao.get("test"));
    	}
    
    }

        结果很简单:

    test-----ming...

        

        

    文章结束给大家分享下程序员的一些笑话语录: 手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn

  • 相关阅读:
    springboot 搭建druid数据监控
    spring-boot编写简易mvc
    解决rabbitmq 开启启动报错
    intelij idea 使用maven打包报错 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1
    php foreach循环引用的问题
    手把手编写hyperf JsonRpc demo
    centos8配置nfs教程本机系统mac
    Java基础的练习题
    Java——循环
    Java——数组
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3093451.html
Copyright © 2011-2022 走看看