[BAEL-3614] - Spring Redis Configuration From application.properties File (#9226)

* BAEL-3614 - Initial commit

* BAEL-3614 - Removing unnecessary properties
This commit is contained in:
aitorcuesta
2020-05-03 22:25:39 +02:00
committed by GitHub
parent 31462c6b49
commit b26b71567a
9 changed files with 295 additions and 10 deletions
@@ -0,0 +1,24 @@
package com.baeldung.spring.redis.configuration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import com.baeldung.spring.redis.configuration.entity.Book;
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
@Bean
public RedisTemplate<Long, Book> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Long, Book> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Add some specific configuration here. Key serializers, etc.
return template;
}
}
@@ -0,0 +1,19 @@
package com.baeldung.spring.redis.configuration;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringRedisConfigurationApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringRedisConfigurationApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}
@@ -0,0 +1,32 @@
package com.baeldung.spring.redis.configuration.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.spring.redis.configuration.entity.Book;
import com.baeldung.spring.redis.configuration.repository.BooksRepository;
@RestController
@RequestMapping("/books")
public class BooksController {
@Autowired
private BooksRepository repository;
@PostMapping("/create")
public String newBook(@RequestBody Book newBook) {
repository.save(newBook);
return "Added";
}
@GetMapping("/get/{id}")
public Book findOne(@PathVariable Long id) {
return repository.findById(id);
}
}
@@ -0,0 +1,46 @@
package com.baeldung.spring.redis.configuration.entity;
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Book() {
}
public Book(String name) {
this.name = name;
}
public Book(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + "]";
}
}
@@ -0,0 +1,25 @@
package com.baeldung.spring.redis.configuration.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.baeldung.spring.redis.configuration.entity.Book;
@Component
public class BooksRepository {
@Autowired
private RedisTemplate<Long, Book> redisTemplate;
public void save(Book book) {
redisTemplate.opsForValue()
.set(book.getId(), book);
}
public Book findById(Long id) {
return redisTemplate.opsForValue()
.get(id);
}
}