Application and Testing Context with Multi-Project Dependencies in Spring

We’re going to do some fun things with Jsoup in the next release of our NixMash Spring app so we added a new jsoup Module and got to work.

We want to leverage other Spring modules while being able to use jsoup as an independent app. We’ll use database services in our jpa project along with Velocity templating and other services in our mail module. As a sidebar, the jsoup module will in the future become a dependency of the mvc web project, but we’re not there yet.

We start by adding our module dependencies in build.gradle.

Creating Application Context

Here’s a common approach to creating Application Context in Spring Boot.

Really straightforward, but with our dependency situation we’re going to encounter a big fat fail with errors like this one.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nixmash.springdata.mail.service.JsoupService] found for dependency.

To bring our Spring dependency projects into the fold we’re going to load an implementing class of ApplicationContext called AnnotationConfigApplicationContext. From the Spring Docs:

Standalone application context, accepting annotated classes as input – in particular @Configuration-annotated classes, but also plain @Component types and JSR-330 compliant classes using javax.inject annotations.

We’ll define the configuration classes for each of the projects that comprise our Spring Application in our AnnotationConfigApplicationContext.

JUnit Context for a Multiple Spring Dependency Project

We need to do a similar thing to configure our Testing Environment where we list the Configuration files of our three Spring Projects and use AnnotationConfigContextLoader as our loader.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.