<!-- 集群版配置 --> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="com.lamsey.jedis.JedisClientCluster"> <property name="jedisCluster" ref="jedisCluster"/> </bean>
首先配置:集群的连接地址
查看JedisCluster的源码:
参数名为nodes的set集合
public JedisCluster(Set<HostAndPort> nodes) {
this(nodes, DEFAULT_TIMEOUT);
}
1.所以采用构造体赋值,构造体内采用set集合:
<constructor-arg name="nodes"> <set>
......
</set> </constructor-arg>
2.HostAndPort属于另外的类,所以创建bean:
查看源码,需要配置两个属性:
可以用propety和构造器两种方法配置
public HostAndPort(String host, int port) { this.host = host; this.port = port; }
<bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.128"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean>
最后与实现类结合起来使用:
<bean id="jedisClientCluster" class="com.lamsey.jedis.JedisClientCluster"> <property name="jedisCluster" ref="jedisCluster"/> </bean>
因为实现类里面有jedisCluster的set方法,所以能够用property进行赋值。
public class JedisClientCluster implements JedisClient{ private JedisCluster jedisCluster; public JedisCluster getJedisCluster() { return jedisCluster; } public void setJedisCluster(JedisCluster jedisCluster) { this.jedisCluster = jedisCluster; } @Override public String set(String key, String value) { return jedisCluster.set(key, value); } }
完成集群的连接地址配置后,
就可以用
jedisCluster.set(key, value);进行操作了。
通过这个过程分析,算是比较清楚了解了bean和java类之间的联系了,哈哈哈。