RedisTemplate injection failed

private static final Logger log = LoggerFactory.getLogger (MybatisRedisCache.class);

// 
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
@Autowired
private StringRedisTemplate redisTemplate;

private String id;

public MybatisRedisCache(final String id) {
    if (id == null) {
        throw new IllegalArgumentException("Cache instances require an ID");
    }
    this.id = id;
}

@Override
public String getId() {
    return this.id;
}

@Override
public void putObject(Object key, Object value) {
    if (value != null) {
        redisTemplate.opsForValue().set(key.toString(), value.toString());
    }
}

@Override
public Object getObject(Object key) {
    try {
        if (key != null) {
            return redisTemplate.opsForValue().get(key.toString());
        }
    } catch (Exception e) {
        log.error(" ");
    }
    return null;
}

@Override
public Object removeObject(Object key) {
    if (key != null) {
        redisTemplate.delete(key.toString());
    }
    return null;
}

@Override
public void clear() {
    log.debug("");
    Set<String> keys = redisTemplate.keys("*:" + this.id + "*");
    if (!CollectionUtils.isEmpty(keys)) {
        redisTemplate.delete(keys);
    }
}

@Override
public int getSize() {
    Long size = redisTemplate.execute((RedisCallback<Long>) RedisServerCommands::dbSize);
    return size.intValue();
}

@Override
public ReadWriteLock getReadWriteLock() {
    return this.readWriteLock;
}-sharp-sharp-sharp 

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

May.08,2022

it is not clear whether objects of this class StringRedisTemplate are managed within the framework, but there are RedisTemplate (after you introduce the corresponding spring-boot-starter-data-redis jar package)


do you think this class is managed by ioc?


suggest posting bean config (java config or XML) files so that it is easier to locate the problem, and spring officials do not recommend this kind of field injection. It is recommended to use constructors for dependency injection


first confirm what framework you are using

    For more information on how to configure springboot, please see the official document https://github.com/spring-pro....
  1. spring, then go to the official website to see how to use the configuration

Please provide the complete code, and describe the problem clearly. Neatly typesetting


suggest that you first take a look at how to ask


Please provide the complete code


I have exactly the same problem with you, the code is the same. May I ask how to solve


now look back at the problems you encountered at that time, ha.

Menu