redis configuration and yml, properties visit

Configuring two bean:

JedisConnectionFactory 和 RedisTemplate 
JedisConnectionFactory factory = new JedisConnectionFactory();//redisSentinelConfiguration()
try {
    Yaml yaml = new Yaml();
    InputStream resourceAsStream = SMSUtil.class.getClassLoader().getResourceAsStream("application.yml");
    JSONObject redisObj = JSONObject.parseObject(JSONObject.toJSONString( yaml.load(resourceAsStream))).getJSONObject("spring").getJSONObject("redis");
    factory.setHostName(redisObj.getString("host" )); 
    Factory.setPort (redisObj.getInteger ( " Port " )); 
    factory.setPassword (redisObj.getString ( " password " )); 
} the catch (Exception E) { 
    e.printStackTrace (); 
    . The System OUT .println (e.getMessage ()); 
} 
// constructor StringRedisTemplate the default settings stringSerializer 


redisTemplate = new new RedisTemplate <> ();
 // set to open transaction 
redisTemplate.setEnableTransactionSupport ( to true );
 // sET Key Serializer
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
redisTemplate.setConnectionFactory(factory);
redisTemplate.afterPropertiesSet();

 

1.java read yml

Yaml yaml = new Yaml();
InputStream resourceAsStream = SMSUtil.class.getClassLoader().getResourceAsStream("config/commom.yml");
JSONObject redisObj = JSONObject.parseObject(JSONObject.toJSONString(yaml.load(resourceAsStream)));

2.java读取properties: https://www.cnblogs.com/sebastian-tyd/p/7895182.html

Properties.getProperty (String Key);
 // the config file name for the attribute, in a bag com.test.config, if it is placed under src, can directly config   
ResourceBundle Resource the ResourceBundle.getBundle = ( " COM / Test / config / config " ); 
String Key = resource.getString ( " keyWord " );

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/big-cut-cat/p/12001487.html