Java 如何自动连接模板<;长字符串>;
我想在spring boot上使用RedisTemplate。 我可以成功地使用StringRedistemplate,但当我不能使用RedisTemplate时。 这是代码Java 如何自动连接模板<;长字符串>;,java,spring,spring-mvc,redis,Java,Spring,Spring Mvc,Redis,我想在spring boot上使用RedisTemplate。 我可以成功地使用StringRedistemplate,但当我不能使用RedisTemplate时。 这是代码 @Service public class MyService { @Autowired private RedisTemplate<String, Long> template; public void execute() { template.opsForValu
@Service
public class MyService {
@Autowired
private RedisTemplate<String, Long> template;
public void execute() {
template.opsForValue().set("hoge", 1l);
}
}
stacktrace表明您尚未定义要用于在
RedisTemplate
中注入的Bean。您可以通过创建配置文件来解决它,例如
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class AppConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
RedisTemplate< String, Long > redisTemplate() {
final RedisTemplate< String, Long > template = new RedisTemplate< String, Long >();
template.setConnectionFactory( jedisConnectionFactory() );
template.setKeySerializer( new StringRedisSerializer() );
template.setHashValueSerializer( new GenericToStringSerializer< Long >( Long.class ) );
template.setValueSerializer( new GenericToStringSerializer< Long >( Long.class ) );
return template;
}
}
如果您使用SpringBoot,那么向SpringBootStarterRedis添加一个依赖项,这将引入RedisTemplatebean。但是,该bean的类型是
RedisTemplate
因此,为您需要的操作创建另一个bean,如图所示,您可以按照顶部所述的操作,但您可以轻松地处理: 1、自动连线
RedisTemplate
@Autowired
private RedisTemplate redisTemplate;
2、设置如下值:
redisTemplate.opsForValue().set("yourkey", 12434L);
(Long) redisTemplate.opsForValue().get("yourkey");
3、获得如下值:
redisTemplate.opsForValue().set("yourkey", 12434L);
(Long) redisTemplate.opsForValue().get("yourkey");
无需创建类似bean的
RedisTemplate
,如果这样做,可能会在系统中创建很多bean。所以就用简单的方法吧。给一些不同的东西,就像现在的2018年一样(有些东西变化很大,在很长一段时间里阻碍了我)
- 第一个
依赖项
(渐变)
[Linux]
redis-server
[spring.properties]
spring.redis.host=localhost
spring.redis.port=6379
spring.cache.cache-names=orderIdPool,users
spring.cache.redis.cache-null-values=true
spring.cache.redis.time-to-live=600000ms
@Configuration
public class RedisConfig {
/**
* create a lettuceConnectionFactory for redisTemplate {@link
#redisUserTemplate()}
* @return LettuceConnectionFactory, the old version use Jedis
*/
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory();
}
/**
* Description: aiming to create a template for Long and user(To be a user cache)
* @return redisTemplate<Long,User>
*/
@Bean
@Qualifier("redisUserTemplate")
public RedisTemplate<Long, User> redisUserTemplate() {
RedisTemplate<Long, User> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(new LettuceConnectionFactory());
return redisTemplate;
}
private RedisTemplate redisTemplate
//@Qualifier to choose the bean you actually want.
@Autowired
public constructor(@Qualifier("xxxRedisTemplate")RedisTemplate redisTemplate){
this.redisTemplate=redisTemplate;
}
private HashOperations<Long, byte[], byte[]> hashOperations = redisUserTemplate.opsForHash();
private HashMapper<Object, byte[], byte[]> hashMapper = new ObjectHashMapper();
@Nullable
public void saveObject(Long key, User user) {
Map<byte[], byte[]> mappedHash = hashMapper.toHash(user);
hashOperations.putAll(key, mappedHash);
}
public Object loadObject(Long key) {
Map<byte[], byte[]> loadedHash = hashOperations.entries(key);
return hashMapper.fromHash(loadedHash);
}
private HashOperations HashOperations=redisUserTemplate.opsForHash();
私有HashMapper HashMapper=新对象HashMapper();
@可空
公共void saveObject(长键,用户){
Map mappedHash=hashMapper.toHash(用户);
putAll(key,mappedHash);
}
公共对象loadObject(长键){
Map loadedHash=hashOperations.entries(键);
返回hashMapper.fromHash(loadedHash);
}
您是否在xml中定义了bean?如果是,那么你能粘贴你粘贴相关的代码吗?我不使用xml。我在spring boot上使用自动配置。你能粘贴你的spring boot配置吗?你在那里定义了你的redistemplate?非常感谢你的回复。我创建了配置文件,然后我就可以使用RedisTemplate了。谢谢!但是为什么要添加一个独立的jedisConnectionFactory bean,为什么不能只
template.setConnectionFactory(新的jedisConnectionFactory())
@zhuguowei如果您不想在许多地方使用/autowireJedisConnectionFactory
,那么请随意使用内联it@sol4me,我可以在applicationContext.xml中配置bean吗?我这么做了,但没用。您能帮我看一下我的问题吗?它是用xml配置的?链接使用原始类型不是一个好主意。OP希望使用泛型类型
private HashOperations<Long, byte[], byte[]> hashOperations = redisUserTemplate.opsForHash();
private HashMapper<Object, byte[], byte[]> hashMapper = new ObjectHashMapper();
@Nullable
public void saveObject(Long key, User user) {
Map<byte[], byte[]> mappedHash = hashMapper.toHash(user);
hashOperations.putAll(key, mappedHash);
}
public Object loadObject(Long key) {
Map<byte[], byte[]> loadedHash = hashOperations.entries(key);
return hashMapper.fromHash(loadedHash);
}