spring中集成使用jedis(1)

本文主要说明在项目中通过spring集成jedis的使用方法。

首先,在jedis的连接配置中,出于性能考虑选择连接池形式。
jedis中的连接池类为

redis.clients.jedis.JedisPool

其中包含多个构造器,可根据需要自行选取,这里选择较为基础形式:

public JedisPool(final GenericObjectPoolConfig poolConfig,
final String host, final int port) {
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE, null);
}

spring中配置如下:

<!-- 连接池配置形式、端口为默认:6379 -->
<bean id="redisPool" class="redis.clients.jedis.JedisPool"
singleton="true" lazy-init="true">
<!-- 连接池配置 -->
<constructor-arg index="0">
<ref bean="poolConfig" />
</constructor-arg>
<!-- host -->
<constructor-arg index="1" value="127.0.0.1" />
<!-- port -->
<constructor-arg index="2" value="6379" type="int" />
</bean>

其中涉及到连接池配置poolconfig,jedis的连接池配置主要基于commons-pool2-2.0,涉及类为

org.apache.commons.pool2.impl.GenericObjectPoolConfig

包含maxTotal、maxIdle、minIdle、maxWaitMillis属性,可通过spring配置如下:

	<!-- 连接池设置 -->
<bean id="poolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
<property name="maxTotal" value="8" />
<property name="maxIdle" value="8" />
<property name="minIdle" value="0" />
<property name="maxWaitMillis" value="-1" />
</bean>

至此,便可以方便的将连接池作为属性注入到对应的redis服务封装对象中,如:

<!-- redis服务 -->
<bean id="redisService"
class="com.service.imp.RedisService"
lazy-init="true">
<property name="jedisPool">
<ref local="redisPool" />
</property>
</bean>

然后就可以直接通过连接池获取jedis,方便快捷的操作redis了。

后续会添加master/salve配置方式,敬请期待。。。

时间: 2024-08-29 20:11:46

spring中集成使用jedis(1)的相关文章

spring中集成使用jedis(2)

本文主要就spring注入的连接池使用问题,做简要说明. 使用过JedisPool的同学会发现,通过JedisPool获取资源,在使用完毕后,需要显式的将资源放回连接池中, 如下: JedisPool jedisPool; Jedis jedis = jedisPool.getResource(); //操作 jedisPool.returnResource(jedis); 如果未显示的回收资源,则在连接池中资源使用完毕后,系统会出现阻塞. 因为如果使用连接池,就相当于将客户端连接托管给池,而池

spring 中集成quartz定时器及quartz中cronExpression配置说明

 spring 中集成quartz: spring文件的配置: <?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:p="http:/

spring中集成shiro进行安全管理

shiro是一款轻量级的安全框架,提供认证.授权.加密和会话管理四个基础功能,除此之外也提供了很好的系统集成方案. 下面将它集成到之前的demo中,在之前spring中使用aop配置事务这篇所附代码的基础上进行集成 一.添加jar包引用 修改pom.xml文件,加入: <!-- security --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core&

基于Maven在Spring中集成CXF Web Service框架

引言: 在跨系统和跨平台的系统通信中,WebService是一个事实上的标准,其以平台无关性,获得了广泛的应用.本文将讲述如何基于Spring来集成CXF,并开发出第一个Hello World的应用. 1.  Web Service是什么? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web Service技术, 能使

在Spring中集成shiro

1.在pxm添加集成导入 <!-- shiro的支持包 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.4.0</version> <type>pom</type> </dependency> <!-- shiro与Spr

Spring中集成Shiro授权实例

授权流程回顾 首先说一句,使用授权的前提当然是先要实现身份验证,也就是要保证用户登录之后才可能考虑授权的问题.关于身份验证之前已经写过了,还不清楚的童鞋可以点这里 上一篇文章介绍了Shiro中授权的一些基础知识和原理.学了就要用,本篇文章就介绍如何在项目中应用Shiro的授权.这里为了方便大家阅读,先贴出上一篇文章中分析出的授权流程: 当我们调用Subject.hasRole(...)后 首先会委托给securityManager来处理,而securityManager内部有一个Authoriz

关于spring 中集成shrio注意点

项目中需要包含shrio包 WebContent.WEB-INF.lib.shrio-all-1.2.1.jar 在项目的web.xml文件中加入 <filter>    <filter-name>shiroFilter</filter-name>    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    <init-par

细说shiro之五:在spring框架中集成shiro

官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${version.shiro}</version> </dependency&g

Spring Boot集成Jasypt安全框架

Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPlaceholderConfigurer类作为属性替换配置类,这里Spring集成Jasypt则使用Jasypt对属性替换配置类的实现.EncryptablePropertySourcesPlaceholderConfigurer. 在Spring中集成比较容易,而且Jasypt官方也给出了配置Bea