使用RedisTemplate时要加上泛型参数,否则可能报找到2个,使@Autowired注解失败
package tju.SpringBootRedisStudy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CallbackController {
@Autowired
private RedisTemplate<String,String> template;
@RequestMapping("/callback")
public String callback() {
template.execute(new RedisCallback() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
((StringRedisConnection)connection).set("hi","lu");
return null;
}}
);
return "ok";
}
}