1

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

1
  • Method invocation 'opsForValue' will produce 'NullPointerException' Commented Dec 8, 2020 at 3:20

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

I can barely understand you, to be frank this is the first time me using spring-related stuff, could you please show me exactly how to correct this?
@Django47 Make the template a parameter on your class's constructor like you would do if you had no Spring in the first place. (And get in the habit of never using @Autowired on fields, because it leads to problems like this; just pass anything you need in the constructor.)
I've change my code to private ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class); @SuppressWarnings("unchecked") private RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("redisTemplate"); private ValueOperations<String, Object> operations = redisTemplate.opsForValue(); and got new errorNo CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.
after removing @EnableCaching the programe finally runs without error! but I still need to know how the code get the yml or properties metrics
fanally using @Value("${spring...}")worked for me...
0

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.

1 Comment

Yes, Lombok could help here, but we don't have information about Lombok dependencies in the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.