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

View File

@ -1,19 +1,22 @@
package org.baeldung.spring.data.redis.config; package org.baeldung.spring.data.redis.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@Configuration @Configuration
@ComponentScan("org.baeldung.spring.data.redis")
public class RedisConfig { public class RedisConfig {
@Bean @Bean
JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory(); return new JedisConnectionFactory();
} }
@Bean @Bean
public RedisTemplate< String, Object> redisTemplate() { public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate< String, Object> template = new RedisTemplate<String, Object>(); final RedisTemplate< String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory()); template.setConnectionFactory(jedisConnectionFactory());
return template; return template;

View File

@ -1,6 +1,7 @@
package org.baeldung.spring.data.redis.repo; package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student; import org.baeldung.spring.data.redis.model.Student;
import org.springframework.stereotype.Component;
import java.util.Map; import java.util.Map;

View File

@ -1,6 +1,7 @@
package org.baeldung.spring.data.redis.repo; package org.baeldung.spring.data.redis.repo;
import org.baeldung.spring.data.redis.model.Student; 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.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -11,26 +12,23 @@ public class StudentRepositoryImpl implements StudentRepository {
private static final String KEY = "Student"; private static final String KEY = "Student";
@Autowired
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void saveStudent(final Student student) { 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) { 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) { 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() { public Map<Object, Object> findAllStudents() {
return this.redisTemplate.opsForHash().entries(KEY); return redisTemplate.opsForHash().entries(KEY);
} }
public void deleteStudent(final String id) { public void deleteStudent(final String id) {

View File

@ -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>

View File

@ -5,6 +5,7 @@ import org.baeldung.spring.data.redis.model.Student;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -14,34 +15,27 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = { "classpath:/spring-config.xml" })
@ContextConfiguration(classes = RedisConfig.class) @ContextConfiguration(classes = RedisConfig.class)
public class StudentRepositoryTest { public class StudentRepositoryTest {
private StudentRepositoryImpl studentRepositoryImpl; @Autowired
private StudentRepository studentRepository;
@Before
public void setUp(){
studentRepositoryImpl = new StudentRepositoryImpl();
RedisConfig redisConfig = new RedisConfig();
studentRepositoryImpl.setRedisTemplate(redisConfig.redisTemplate());
}
@Test @Test
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepositoryImpl.saveStudent(student); studentRepository.saveStudent(student);
final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); final Student retrievedStudent = studentRepository.findStudent(student.getId());
assertEquals(student.getId(), retrievedStudent.getId()); assertEquals(student.getId(), retrievedStudent.getId());
} }
@Test @Test
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepositoryImpl.saveStudent(student); studentRepository.saveStudent(student);
student.setName("Richard Watson"); student.setName("Richard Watson");
studentRepositoryImpl.saveStudent(student); studentRepository.saveStudent(student);
final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); final Student retrievedStudent = studentRepository.findStudent(student.getId());
assertEquals(student.getName(), retrievedStudent.getName()); assertEquals(student.getName(), retrievedStudent.getName());
} }
@ -49,18 +43,18 @@ public class StudentRepositoryTest {
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
studentRepositoryImpl.saveStudent(engStudent); studentRepository.saveStudent(engStudent);
studentRepositoryImpl.saveStudent(medStudent); studentRepository.saveStudent(medStudent);
final Map<Object, Object> retrievedStudent = studentRepositoryImpl.findAllStudents(); final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
assertEquals(retrievedStudent.size(), 2); assertEquals(retrievedStudent.size(), 2);
} }
@Test @Test
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepositoryImpl.saveStudent(student); studentRepository.saveStudent(student);
studentRepositoryImpl.deleteStudent(student.getId()); studentRepository.deleteStudent(student.getId());
final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId()); final Student retrievedStudent = studentRepository.findStudent(student.getId());
assertNull(retrievedStudent); assertNull(retrievedStudent);
} }
} }