skip to Main Content

config:

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{

    @Bean(name = "template")
    public RedisTemplate<String, Object> template(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);
        StringRedisSerializer stringSerial = new StringRedisSerializer();
        template.setKeySerializer(stringSerial);
        template.setValueSerializer(jacksonSeial);
        template.setHashKeySerializer(stringSerial);
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();
        return template;
    }

}

use:

@Autowired
    private RedisTemplate<String, Object> template;

and this

  ValueOperations<String, Object> operations = template.opsForValue();

always get a error, warn
stack trace is:

java.lang.NullPointerException: null
    at cn.com.agree.afa.jcomponent.CacheOperationImpl.<init>(CacheOperationImpl.java:15) ~[afa-interface-1.0.0.jar:na]
    at tc.bank.base.B_MemaryHandle.getCache(B_MemaryHandle.java:36) ~[afa-component-1.0.0.jar:na]
    at tc.bank.base.B_MemaryHandle.B_PutGlobalCache(B_MemaryHandle.java:64) ~[afa-component-1.0.0.jar:na]
    at tc.bank.base.B_LoadData.B_LoadErrorCodeConfig(B_LoadData.java:146) [afa-component-1.0.0.jar:na]
    at cn.com.agree.afa.trade.AimServer.BASE_startup.execute(BASE_startup.java:53) [main/:na]
    at cn.com.agree.trade.TradeManager.execute(TradeManager.java:43) [afa-interface-1.0.0.jar:na]
    at cn.com.agree.afa.App.run(App.java:71) [main/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) [spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) [spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) [spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) [spring-boot-2.4.0.jar:2.4.0]
    at cn.com.agree.afa.App.main(App.java:40) [main/:na]

I just want to use spring boot date redis to do some CRUD operations like template<String, Object>.
please help me

2

Answers


  1. Field injection can’t happen until after the constructor is already finished. Make the template a constructor parameter instead (and avoid field injection generally).

    Login or Signup to reply.
  2. Adding to chrylis answer here.

    Use lombok’s @RequiredArgsConstructor on top of your @Component class. It will generate a constructor with the required fields. Redis template will be initialized and null error will go away.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search