spring @Configuration bean 參數注入

Spring @Value 設置默認值html

Spring 3 supports ${my.server.port:defaultValue} syntax.java

@Value("${zookeeper.enable:false}")
private boolean isZkEnable;node

添加redis.properties文件redis

    spring.redis.cluster.nodes=10.48.193.201:7389,10.48.193.201:7388spring

    spring.redis.cluster.timeout=2000spa

    spring.redis.cluster.max-redirects=8.net

編寫初始化JedisConnectionFactory鏈接工廠的java類code

    @Configurationserver

    public class MonitorConfig {htm

       @Value("${spring.redis.cluster.nodes}")

       private String clusterNodes;

       @Value("${spring.redis.cluster.timeout}")

       private Long timeout;

      @Value("${spring.redis.cluster.max-redirects}")

       private int redirects;

       @Bean

       public RedisClusterConfiguration getClusterConfiguration() {

            Map<String, Object> source = new HashMap<String, Object>();

            source.put("spring.redis.cluster.nodes", clusterNodes);

            source.put("spring.redis.cluster.timeout", timeout);

            source.put("spring.redis.cluster.max-redirects", redirects);

           return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));

          }

       @Bean

       public JedisConnectionFactory getConnectionFactory() {

           return new JedisConnectionFactory(getClusterConfiguration());

          }

      @Bean

       public JedisClusterConnection getJedisClusterConnection() {

           return (JedisClusterConnection) getConnectionFactory().getConnection();

          }

       @Bean

       public RedisTemplate getRedisTemplate() {

           RedisTemplate clusterTemplate = new RedisTemplate();

           clusterTemplate.setConnectionFactory(getConnectionFactory());

           clusterTemplate.setKeySerializer(new DefaultKeySerializer());

           clusterTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());

           return clusterTemplate;

          }

       }

相關文章
相關標籤/搜索