From ecf5baea13bc5c2451d2bc48aec386799ae3f833 Mon Sep 17 00:00:00 2001 From: David Morley Date: Mon, 25 Jan 2016 07:25:48 -0600 Subject: [PATCH] Fix Spring configuration for Spring Data Redis example --- .../spring/data/redis/config/RedisConfig.java | 5 ++- .../data/redis/repo/StudentRepository.java | 1 + .../redis/repo/StudentRepositoryImpl.java | 14 ++++---- .../src/main/resources/spring-config.xml | 19 ----------- .../redis/repo/StudentRepositoryTest.java | 34 ++++++++----------- 5 files changed, 25 insertions(+), 48 deletions(-) delete mode 100644 spring-data-redis/src/main/resources/spring-config.xml diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java index 83244a4d94..a7e75a438a 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/config/RedisConfig.java @@ -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 redisTemplate() { final RedisTemplate< String, Object> template = new RedisTemplate(); template.setConnectionFactory(jedisConnectionFactory()); return template; diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java index 59b3a4c5e8..6a909ed137 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepository.java @@ -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; diff --git a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java index 020a96f1e4..55e6ad5edc 100644 --- a/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java +++ b/spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java @@ -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 redisTemplate; - public void setRedisTemplate(RedisTemplate 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 findAllStudents() { - return this.redisTemplate.opsForHash().entries(KEY); + return redisTemplate.opsForHash().entries(KEY); } public void deleteStudent(final String id) { diff --git a/spring-data-redis/src/main/resources/spring-config.xml b/spring-data-redis/src/main/resources/spring-config.xml deleted file mode 100644 index 4ca240d3f1..0000000000 --- a/spring-data-redis/src/main/resources/spring-config.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java index 09bc74e5cf..08540abd36 100644 --- a/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java +++ b/spring-data-redis/src/test/java/org/baeldung/spring/data/redis/repo/StudentRepositoryTest.java @@ -5,6 +5,7 @@ import org.baeldung.spring.data.redis.model.Student; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -14,34 +15,27 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @RunWith(SpringJUnit4ClassRunner.class) -//@ContextConfiguration(locations = { "classpath:/spring-config.xml" }) @ContextConfiguration(classes = RedisConfig.class) public class StudentRepositoryTest { - private StudentRepositoryImpl studentRepositoryImpl; - - @Before - public void setUp(){ - studentRepositoryImpl = new StudentRepositoryImpl(); - RedisConfig redisConfig = new RedisConfig(); - studentRepositoryImpl.setRedisTemplate(redisConfig.redisTemplate()); - } + @Autowired + private StudentRepository studentRepository; @Test public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepositoryImpl.saveStudent(student); - final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); assertEquals(student.getId(), retrievedStudent.getId()); } @Test public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepositoryImpl.saveStudent(student); + studentRepository.saveStudent(student); student.setName("Richard Watson"); - studentRepositoryImpl.saveStudent(student); - final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); assertEquals(student.getName(), retrievedStudent.getName()); } @@ -49,18 +43,18 @@ public class StudentRepositoryTest { public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); - studentRepositoryImpl.saveStudent(engStudent); - studentRepositoryImpl.saveStudent(medStudent); - final Map retrievedStudent = studentRepositoryImpl.findAllStudents(); + studentRepository.saveStudent(engStudent); + studentRepository.saveStudent(medStudent); + final Map retrievedStudent = studentRepository.findAllStudents(); assertEquals(retrievedStudent.size(), 2); } @Test public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepositoryImpl.saveStudent(student); - studentRepositoryImpl.deleteStudent(student.getId()); - final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); + studentRepository.saveStudent(student); + studentRepository.deleteStudent(student.getId()); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); assertNull(retrievedStudent); } } \ No newline at end of file