Spring Boot component scanning for test libraries

Recently I had an issue where I was trying to use a custom test library I had built in Spring Boot within an existing Spring Boot app. I ran into a weird issue where by using the test library I broke the main app.

The issue was that the main app was trying to initialise the test library before it was ready, it was attempting to autowire beans within it… turned out the issue was because the test library used the same base package naming convention as the main app.

For example, if the main app was com.alanfeekery.app and the test library was com.alanfeekery.test and my application-context.xml looked like this:

<context:component-scan base-package="com.alanfeekery"></context:component-scan>

Then Spring would just go ahead and try configure everything under the package com.alanfeekery, this is bad as the app shouldn’t be configuring the test library. The solution looks like this:

<context:component-scan base-package="com.alanfeekery">
	<context:include-filter type="regex" expression=".*app"/>
	<context:exclude-filter type="regex" expression=".*test"/>

With these include & exclude filters my app no longer scanned into my test library automatically and everything worked as expected.


Another thing to be aware about, if are including a custom test library in your Spring Boot app, make sure you flag the dependency scope to test this means that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phase.

Here’s an example:



Spring Security web classes are not available error

Another random error/bug I came across playing around with Spring MVC, when implementing security I kept getting an error in my security context xml file telling me that required spring security web classes are not available.

While I had spring-security-config in my pom.xml file it seems one of the benefits of Maven had again sh*t itself and a required dependency was not being pulled in.

You need to manually add spring-security-web which solved the issue for me, seems to be a known bug.


Make sure you update the version to match your spring-security-config dependency.

Source: http://stackoverflow.com/a/9677714/1445352

Dmaven multiModuleProjectDirectory system propery is not set

This error just drove me mad in STS/Eclipse.

You need to add the Default VM argument -Dmaven.multiModuleProjectDirectory=$M2_HOME.

You do this by going to Window > Preference > Java > Installed JREs > Edit


This assumes you have the Environment variable M2_HOME set up correctly (this should be done when installed Maven).

Source: Stack Overflow