Version 0.2.4 of NixMash Spring goes deep into Solr Repository Configurations where we demonstrate four different approaches. Here is a screenshot of the repositories in v0.2.4.
We’ll describe them as the following:
- Custom Repository extending SolrCrudRepository and the only repository with a @Repository Annotation
- A Repository extending SimpleSolrRepository whose Bean is created outside of the Repository
- Derived Class extending SolrCrudRepository whose repository bean is created through its Implementation Class Constructor outside of the Repository
- A SolrRepositoryFactoryBean adding custom query support to multiple Model Types
Custom Solr @Repository
We’re going to start with a Custom Solr Repository extending SolrCrudRepository and which is our sole example created with a @Repository annotation.
The class and interfaces in our Custom Solr Repository are below. We’re hanging a @Repository annotation on our CustomProductRepositoryImpl class, which implements CustomBaseRepository where our custom methods are placed. Our CustomProductRepository Interface extends both the CustomBaseRepository and SolrCrudRepository. A note here, it’s important to follow proper file naming conventions with the Implementation Class being the same name as its interface with “Impl” added. You’ll also notice in this repository configuration, SolrTemplate is a bean annotated with @Resource and defined within the Repository.
Simple Solr Repository
We will next look at the Simple Solr Repository approach where we extend SimpleProductRepository and wire-up the Bean outside of the repository.
Let’s look at a SolrConfiguration class that is used in this and the remaining Repository Examples. At (1) we’re creating the SimpleProductRepository Bean with its constructor and pass in the SolrTemplate object. (We’ll look at (2) and (3) later.)
Here are our Simple Solr Repository class and interface declarations. We’re extending SimpleSolrRepository and using @NoRepositoryBean since we wired-up the Bean earlier.
Derived Custom Repository with Implementation Class Constructor
Our third configuration option is a “Derived Class Configuration” where we will be creating the bean outside of the Repository.
What’s interesting in this approach is that we use a constructor in DerivedBaseRepositoryImpl to wire-up a SolrOperations template. We’ll call the constructor from our SolrConfiguration class which we’ll see in a minute.
Back to our SolrConfiguration class and you can see at (2) where we’re wiring up the Bean using its DerivedBaseRepositoryImpl(SolrOperations) Constuctor.
This is an interesting configuration because it supports custom methods for any Model Type, not just Products.
I don’t want to display the SolrConfiguration class a third time so scroll up a bit and we’ll cover how we define the Bean for use in our application. At (3) in the Annotations section we assign MySolrRepositoryFactoryBean as our repositoryFactoryBeanClass property of @EnableSolrRepositories.
Now onto the classes that make up the SolrRepositoryFactoryBean repository. You’ll see that only FactoryProductRepository is Type-specific. We could add a FactoryBookRepository, for instance, and use the same classes for our custom Book Solr queries.
The MySolrRepositoryFactoryBean class is wired-up in @Override doCreateRepositoryFactory().
Wow, that was a lot and we could have gone deeper, but we covered the essentials.
The source code of the four Repository Configurations, along with Service implementation of each, is in the v0.2.4 branch of NixMash Spring on GitHub.