PackageCom.ldr.bean;ImportJava.lang.reflect.Field;ImportRedis.clients.jedis.JedisShardInfo; Public classMyjedisinfo {String host; intPort; intDB; PublicJedisshardinfo newinstance ()throwsnosuchfieldexception, SecurityException, IllegalArgumentException, illegalaccessexception {JedisShardInfo J Edisshardinfo=NewJedisshardinfo (Host,port); Class<?extendsjedisshardinfo> CLZ =Jedisshardinfo.getclass (); Field Declaredfield= Clz.getdeclaredfield ("DB"); Declaredfield.setaccessible (true); Declaredfield.set (jedisshardinfo, DB); returnJedisshardinfo; } PublicString gethost () {returnhost; } Public voidSethost (String host) { This. Host =host; } Public intGetdb () {returnDB; } Public voidSetdb (intdb) { This. db =DB; } Public intGetport () {returnPort; } Public voidSetport (intPort) { This. Port =Port; } }
The Application.xml in spring are configured as follows
<!--Spring Integrated Redis - <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig"> < Propertyname= "Maxtotal"> <value>${redis.maxtotal}</value> </ Property> < Propertyname= "Maxidle"> <value>${redis.maxidle}</value> </ Property> < Propertyname= "Testonborrow"value= "true"/> < Propertyname= "Testonreturn"value= "true"/> </Bean> <BeanID= "Shardedjedispool"class= "Redis.clients.jedis.ShardedJedisPool"Scope= "Singleton"> <Constructor-argIndex= "0"ref= "Jedispoolconfig" /> <Constructor-argIndex= "1"> <List> <!--<bean class= "Redis.clients.jedis.JedisShardInfo" > <constructor-arg name= "host" value= "${r Edis.host} "/> <constructor-arg name=" port "value=" ${redis.port} "/> </bean& Gt - <refBean= "Jedisshardinfo"/><!--production environment, please replace the above - </List> </Constructor-arg> </Bean> <!--The following configuration on production please comment out the BEGIN - <BeanID= "Jedisfactory"class= "Com.ldr.bean.MyJedisInfo"> < Propertyname= "Host"value= "${redis.host}"></ Property> < Propertyname= "Port"value= "${redis.port}"></ Property> < Propertyname= "db"value= "${redis.db}"></ Property> </Bean> <BeanID= "Jedisshardinfo"class= "Redis.clients.jedis.JedisShardInfo"Factory-bean= "Jedisfactory"Factory-method= "Newinstance" > </Bean> <!--above configuration on production please comment out end -
Workaround for unable to modify DB in Jedisshardinfo