spring與jedis整合配置

1.引入jedis的jar包
redis

 

<!-- redis -->spring

<dependency>app

    <groupId>redis.clients</groupId>測試

    <artifactId>jedis</artifactId>spa

    <version>2.7.3</version>xml

</dependency>get

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

 

    <!-- POOL -->io

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

        <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);

    }

}

相關文章
相關標籤/搜索