| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

spring与jedis整合配置

1.引入jedis的jar包

 

<!-- redis -->

<dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

    <version>2.7.3</version>

</dependency>

 
2.在spring配置文件中添加配置

 

    <!-- POOL -->

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

        <property name="maxTotal" value="50" />

        <property name="maxIdle" value="10" />

        <property name="maxWaitMillis" value="1000" />

        <property name="testOnBorrow" value="true" />

    </bean>

    <!-- jedis shard -->

    <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">

        <constructor-arg index="0" value="192.168.1.101" />

        <constructor-arg index="1" value="6379" type="int" />

    </bean>

    <!-- jedis shard pool -->

    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">

        <constructor-arg index="0" ref="jedisPoolConfig" />

        <constructor-arg index="1">

            <list>

                <ref bean="jedisShardInfo" />

            </list>

        </constructor-arg>

    </bean>

 

 经过以上两个步骤,即可完成配置

3. 测试

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(value={"/spring/applicationContext.xml""/spring/redis.xml"})

public class RedisTest {

    

    @Autowired

    private ShardedJedisPool shardedJedisPool;

    

    @Test

    public void testSet() {

        ShardedJedis jedis = shardedJedisPool.getResource();

        jedis.set("name""wwss");

    }

    

    @Test

    public void testGet() {

        ShardedJedis jedis = shardedJedisPool.getResource();

        String name = jedis.get("name");

        System.out.println(name);

    }

}


 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1447033987225.html
Redis NoSQL数据库 Jedis