ReviewIn the previous section, we have learned how to setup a Redis server in Windows and Ubuntu. In this section, we will discuss the project's structure and write the Java classes.
Table of ContentsPart 1: Introduction and Functional Specs
Part 2: Redis setup
Part 3: Java classes
Part 4: XML configuration
Part 5: HTML (with AJAX)
Part 6: Running the Application
Project StructureOur application is a Maven project and therefore follows Maven structure. As we create the classes, we've organized them in logical layers: domain, repository, service, and controller.
Here's a preview of our project's structure:
Note: You might have noticed ignore an error icon in the jQuery file. This is an Eclipse validation issue. You can safely ignore this error.
Domain LayerThis layer contains two POJOs, User and Role.
Controller LayerThis layer contains two controllers, MediatorController and UserController
- MediatorController is responsible for redirecting requests to appropriate pages. This isn't really required but it's here for organizational purposes.
- UserController is responsible for handling user-related requests such as adding and deleting of records
Service LayerThis layer contains two services, UserService and InitRedisService
- UserService is our CRUD service for managing users
- InitRedisService is used for initiliazing our database with sample data using the RedisTemplate
As mentioned in Part 1, we shall use Hashes to store Java objects in Redis. With the help of Spring Data for Redis, in particular the RedisTemplate, we're able to perform various Redis operations.
To access Hash operations using RedisTemplate, we use the following syntax:
template.opsForHash() template.opsForHash().put template.opsForHash().delete
To keep track of our users, we will use Set data structure for Redis
template.opsForSet() template.opsForSet().add template.opsForSet().remove
What is Spring Data Redis?
Spring Data for Redis is part of the umbrella Spring Data project which provides support for writing Redis applications. The Spring framework has always promoted a POJO programming model with a strong emphasis on portability and productivity. These values are carried over into Spring Data for Redis.
Utility classesTraceInterceptor class is an AOP-based utility class to help us debug our application. This is a subclass of CustomizableTraceInterceptor (see Spring Data JPA FAQ)
NextIn the next section, we will focus on the configuration files for enabling Spring MVC. Click here to proceed.
Share the joy:
Subscribe by reader Subscribe by email Share