Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot configuration in a multi-Module maven project

I'm having a problem properly setting up spring boot for my multi-module maven project.

There is a module "api" that uses another module "core". Api has an application.properties file that contains spring.mail.host=xxx. According to the spring boot documentation this provides you with a default implementation of the JavaMailSender interface, ready to be autowired.

However the class that is responsible for sending out the e-mails resides in the "core" package. When I try to build that module the build fails because no implementation of JavaMailSender can be found.

My guess then was that the mailing config should reside in "core" in a separate application.properties. I created that and moved the spring.mail.host property from the "api" to the "core" property file.

This time the core module builds successfully, but "api" fails to build because of the same exception, so I think I just moved the problem.

I don't understand the required structure for handling this type of situations well enough so I was wondering what the correct way is for having a "core" module containing all the correct configuration for sending mails and having other modules use the mailing code and config that resides in it.

like image 341
Geoffrey De Vylder Avatar asked Jan 22 '16 08:01

Geoffrey De Vylder


1 Answers

I found the answer in another stack overflow question: How to add multiple application.properties files in spring-boot?

It turns out there can only be 1 application.properties file in the final jar that spring boot creates. To have multiple files you have to rename one of the files to something custom. I named the properties of the core module "core-application.properties".

Then in the API module I added this to the spring boot application class:

@SpringBootApplication
@PropertySource(value = {"core-application.properties", "application.properties"})

Doing this I can correctly use the base properties file and overwrite them in the more specific modules. Also you can still create profile-specific properties file (core-application-production.properties) with this setup, no need to add those to the propertysource manually). Note that @PropertySource does not work for yaml configuration files at this moment.

like image 81
Geoffrey De Vylder Avatar answered Oct 21 '22 15:10

Geoffrey De Vylder