Fix Spring configuration for Spring Data Redis example

This commit is contained in:
David Morley
2016-01-25 07:25:48 -06:00
parent bc7240ead1
commit ecf5baea13
5 changed files with 25 additions and 48 deletions
@@ -1,19 +1,22 @@
package org.baeldung.spring.data.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
@ComponentScan("org.baeldung.spring.data.redis")
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate< String, Object> redisTemplate() {
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate< String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
@@ -1,6 +1,7 @@
package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student;
import org.springframework.stereotype.Component;
import java.util.Map;
@@ -1,6 +1,7 @@
package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
@@ -11,26 +12,23 @@ public class StudentRepositoryImpl implements StudentRepository {
private static final String KEY = "Student";
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void saveStudent(final Student student) {
this.redisTemplate.opsForHash().put(KEY, student.getId(), student);
redisTemplate.opsForHash().put(KEY, student.getId(), student);
}
public void updateStudent(final Student student) {
this.redisTemplate.opsForHash().put(KEY, student.getId(), student);
redisTemplate.opsForHash().put(KEY, student.getId(), student);
}
public Student findStudent(final String id) {
return (Student) this.redisTemplate.opsForHash().get(KEY, id);
return (Student) redisTemplate.opsForHash().get(KEY, id);
}
public Map<Object, Object> findAllStudents() {
return this.redisTemplate.opsForHash().entries(KEY);
return redisTemplate.opsForHash().entries(KEY);
}
public void deleteStudent(final String id) {
@@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true" />
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>
<bean id="studentRepository" class="org.baeldung.spring.data.redis.repo.StudentRepositoryImpl">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>