MicroStream makes it easy to store and load Java objects. Spring Boot is a great start for Crud applications. In this tutorial, we will learn how to combine these two great frameworks based on a simple example.
We use spring-boot-starter-parent for simple dependency management, versioning and plug-in configuration. And we have to include the MicroStream repository in order to access its artifacts. The property
<java.version> must be set to 1.8 because Spring Boot Starter uses 1.6 as default, but MicroStream needs Java 8 at least.
The Domain Layer
With the project set up nicely, let’s now implement a simple domain layer. We will just include a single
Customer class for simplicity's sake.
As you can see there are no annotations, like
@Entity, as well as no
Serializable interface or any other boilerplate stuff. That is the beauty of MicroStream, just use your Pojos as they are. You will be able to perform Crud operations without polluting your domain objects.
The Service Layer
Spring works with the repository pattern. A good way to separate your data and your services. First, we will provide a simple interface which defines the supported actions.
The implementation will utilize MicroStream as a storage layer. The
@Component annotation tells Spring's CDI mechanism that this class can be used as an implementation for the defined customer repository. In the constructor, we start our storage with the root object customers and as a second parameter, we provide the location, where the data should be stored.
@Value is a Spring annotation which injects the argument from the configuration file. To save our data we just call
storage.store(Object). The method
findByFirstName shows the mechanism to obtain data from MicroStream. Instead of writing some query in SQL or other query languages, we just use plain old Java.
To set a value for the
microstream.store.location parameter we will provide a simple configuration file, called
application.properties. It is possible to access system properties, like
user.home, with the dollar syntax.
Running the Application
And at last the Spring application itself. The command-line runner will execute all methods of the service layer. First, the sample data is created, then queries are executed, updates are performed, and at last, the data will be removed again.
Just run this class to see the frameworks in action.
In this tutorial, we learned how to create a simple CRUD application with Spring and MicroStream. As you can see it is pretty straightforward. The complete example is available on GitHub.