spring 配置 redis

1.maven相關pom.xmljava

<dependencies>

        <!--spring redis-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.0.RELEASE</version>
        </dependency>
 
        <!--client 配置-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.3</version>
        </dependency>
</dependencies>

2.application_context.xml相關配置redis

<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="300"/>
        <property name="maxIdle" value="100"/>
        <property name="maxWaitMillis" value="10000"/>
        <property name="testOnBorrow" value="true"/>
    </bean>

    <bean id="jedisConnFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:usePool="true" p:hostName="${host}" p:password="${password}" c:poolConfig-ref="jedisConfig"/>

    <!-- redis template definition -->
    <bean id="redisTemplate"
          class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="jedisConnFactory">
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
    </bean>

3.相關調用spring

@Service("sessionService")
public class SessionService {

    @Autowired
    private RedisTemplate<String, Map<String, Object>> redisTemplate;

    //todo redisTemplate  相關使用
}
相關文章
相關標籤/搜索