#redis配置
redis.server.host=192.168.50.162
redis.server.port=6379
redis.server.password=password
redis.server.timeout=5000
redis.server.maxidle=50
redis.server.maxwaitmillis=5000
redis.server.maxtotal=500
對 redis 配置引數進行讀取和繫結,配置屬性注入到 jedisproperties:
import org.springframework.boot.context.properties.configurationproperties;
@configurationproperties(prefix = jedisproperties.jedis_prefix)
public class jedisproperties
public void settimeout(int timeout)
public string getpassword()
public void setpassword(string password)
public string gethost()
public void sethost(string host)
public int getport()
public void setport(int port)
public int getmaxtotal()
public void setmaxtotal(int maxtotal)
public int getmaxidle()
public void setmaxidle(int maxidle)
public int getmaxwaitmillis()
public void setmaxwaitmillis(int maxwaitmillis)
}
配置了 redis 連線池之後,將 redis 連線池 注入到 redisclient 中,並生成 redisclient bean:
import com.mljr.auth.config.properties.jedisproperties;
import com.mljr.auth.util.redisclient;
import org.slf4j.logger;
import org.slf4j.logge***ctory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
import org.springframework.boot.autoconfigure.condition.conditionalonclass;
import org.springframework.boot.autoconfigure.condition.conditionalo****singbean;
import org.springframework.boot.context.properties.enableconfigurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import redis.clients.jedis.jedispool;
import redis.clients.jedis.jedispoolconfig;
@configuration
@enableconfigurationproperties(jedisproperties.class)
@conditionalonclass(redisclient.class)
public class jedisconfig
@bean
@conditionalo****singbean(redisclient.class)
public redisclient redisclient(@qualifier("jedispool") jedispool pool) ,port={}", prop.gethost(), prop.getport());
redisclient redisclient = new redisclient();
redisclient.setjedispool(pool);
return redisclient;
}}
配置一些常用的 redis 的操作:
import redis.clients.jedis.jedis;
import redis.clients.jedis.jedispool;
public class redisclient catch (exception e)finally
} /**
* 設定過期時間
* @param key
* @param value
* @param exptime
* @throws exception
*/public void setwithexpiretime(string key, string value, int exptime) catch (exception e)finally
} public string get(string key) catch (exception e)finally
return null;
} public jedispool getjedispool()
public void setjedispool(jedispool jedispool)
}
spring session使用配置redis
1.新增依賴 dependency groupid org.springframework.session groupid artifactid spring session data redis artifactid version 1.2.0.release version dependency...
SpringBoot常用配置
前言 springboot整合了主流的第三方框架,但是需要使用springboot那一套配置方式。但是我這裡只列舉了非常非常常用的,可以看已發的幾篇部落格,慢慢會補充。當然官方文件裡也有相應的配置,可惜沒有注釋。spring.resources.cache period 設定資源的快取時效,以秒為單...
Spring boot自動配置
1 從原始碼角度看spring boot 自動配置 這個方法呼叫的是 initialize sources suppresswarnings private void initialize object sources this.webenvironment deducewebenvironment...