Update Spring Data Redis example to use constructor injection
This commit is contained in:
parent
e6cc0bb722
commit
ddec979e4d
|
@ -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.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
|
@ -2,9 +2,11 @@ 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.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.HashOperations;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
|
@ -12,26 +14,36 @@ 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;
|
||||||
|
private HashOperations hashOperations;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public StudentRepositoryImpl(RedisTemplate<String, Object> redisTemplate) {
|
||||||
|
this.redisTemplate = redisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void init() {
|
||||||
|
hashOperations = redisTemplate.opsForHash();
|
||||||
|
}
|
||||||
|
|
||||||
public void saveStudent(final Student student) {
|
public void saveStudent(final Student student) {
|
||||||
redisTemplate.opsForHash().put(KEY, student.getId(), student);
|
hashOperations.put(KEY, student.getId(), student);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateStudent(final Student student) {
|
public void updateStudent(final Student student) {
|
||||||
redisTemplate.opsForHash().put(KEY, student.getId(), student);
|
hashOperations.put(KEY, student.getId(), student);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student findStudent(final String id) {
|
public Student findStudent(final String id) {
|
||||||
return (Student) redisTemplate.opsForHash().get(KEY, id);
|
return (Student) hashOperations.get(KEY, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Object, Object> findAllStudents() {
|
public Map<Object, Object> findAllStudents() {
|
||||||
return redisTemplate.opsForHash().entries(KEY);
|
return hashOperations.entries(KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteStudent(final String id) {
|
public void deleteStudent(final String id) {
|
||||||
this.redisTemplate.opsForHash().delete(KEY, id);
|
hashOperations.delete(KEY, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue