Geoffrey De Vylder
Geoffrey De Vylder

Reputation: 4153

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.

Upvotes: 8

Views: 16525

Answers (4)

Miladesnovakaina
Miladesnovakaina

Reputation: 11

the solution is to define properties files as a value of @PropertiesSource in Starter class. but it is beter to put "classpath:" behind the properties files. for example in Intellij idea after adding the "classpatch:" word berhind the files name, values become to link. like this:

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

I hope to helped you.

Upvotes: 0

Geoffrey De Vylder
Geoffrey De Vylder

Reputation: 4153

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.

Upvotes: 13

Mufanu
Mufanu

Reputation: 534

Your API's pom.xml must has dependency of CORE module.

Upvotes: 0

Thang Hoang
Thang Hoang

Reputation: 380

there is one effective application.properties per project. you just keep 2 properties file for a success build.

when api module use core module, the application.properties in core module is overwrite by api.

Upvotes: 1

Related Questions