Redis is used in springboot
It’s familiar****Template
- SpringBoot does not require manual Template
@Bean
. Classes can inject dependencies directly
Code sample
- The configuration file
Spring. Redis. Host = 192.168.171.131 spring. Redis. Port = 6379Copy the code
- ping-pong
@SpringBootTest
class RedisSpringBoot01ApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void contextLoads(a) {
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
System.out.println(connection.ping());// Use connection ping
System.out.println(redisTemplate.execute(RedisConnectionCommands::ping));2 / / way
}
public String ping(a){
return redisTemplate.execute((RedisCallback<String>) connection -> connection.ping());// The ontology of mode two}}Copy the code
Other usage
- Different types of operations for Redis
opsFor***()
After the method and base operation (add delete change check)
How do you configure template classes, and how do you configure serialization
- Goal: Serializable objects stored in Redis, even if the entity class does not inherit FROM S
erializable
Interface, so change the template class yourself. - Why is it possible to customize template classes? Click the source code and have a look
- Custom template class (create Config class)
@Configuration
public class MyRedisConfig {
@Bean
@SuppressWarnings ( "all")
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);// By instantiating
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// This will make all member fields serializable without further comment, not just public fields (default)
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
/* This value indicates that the default type will be used for all non-final types, except for a few "natural" types (strings, Booleans, integers, doubles), which can be correctly inferred from JSON; And all arrays of non-final type */
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//ObjectMapper is a way to further control the JSON serialization process
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// Key uses string serialization
template.setKeySerializer(stringRedisSerializer);
// Hash hey is also serialized by String
template.setHashKeySerializer(stringRedisSerializer);
//vaLue serialization uses Jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// The hash value serialization method uses Jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
// When all bean properties are set, this method allows the bean instance to perform validation and final initialization of its overall configuration.
template.afterPropertiesSet();
returntemplate; }}Copy the code
Introduce the Utils utility class
- code
Bug collection
@Autowired
andnew
Is something of two worlds, one in Spring (IOC) and one outside
- Break down