J. Sabatier
J. Sabatier

Reputation: 395

Multi-Module spring boot project messages.properties is not loaded

I develop a modular Spring Boot 2 application with this structure :

- cartocontrib (pom project)
|-- cartocontrib-dao (jar project) 
|-- cartocontrib-datastore-postgis (jar project) 
|-- cartocontrib-service (jar project) 
|-- cartocontrib-web (war project)

The module "cartocontrib-datastore-postgis" is a jar that have to define some JSP, and messages.properties

I managed to import the module's JSPs by putting them in "src/main/resources/META-INF/resources" folder of the module.

But I get an error for messages defined in the module messages.properties :

javax.servlet.jsp.JspTagException: No message found under code 'administration.datastores.postgis.create.title' for locale 'fr_FR'.

It look like the messages.properties of the jar module isn't loaded by the application.

Can someone help me to make it work ?

Upvotes: 0

Views: 2418

Answers (2)

J. Sabatier
J. Sabatier

Reputation: 395

Finally found how do do that !

First step : Define a CustomMessageSourceConfiguration class in the main webapp :

package fr.lepuyenvelay.cartocontrib.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class CustomMessageSourceConfiguration {
    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocalValidatorFactoryBean getValidator() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(messageSource());
        return bean;
    }
}

So it will load messages.properties in src/main/resources/messages folder.

Like explained here : https://www.baeldung.com/spring-custom-validation-message-source

Second step : Use @PostConstruct in module's config to add custom messages.properties

package fr.lepuyenvelay.cartocontrib.datastore;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import fr.lepuyenvelay.cartocontrib.service.datastore.DataStoreType;
import fr.lepuyenvelay.cartocontrib.service.datastore.DataStoreTypeManager;

@Configuration
public class CartocontribDatastorePostgisConfig {
    final static Logger LOG = LoggerFactory.getLogger(CartocontribDatastorePostgisConfig.class);

    @Autowired
    DataStoreTypeManager dstManager;

    @Autowired
    ReloadableResourceBundleMessageSource messageSource;

    @PostConstruct
    private void init() {
        dstManager.addSupportedDataStoreType(new DataStoreType("postgis"));
        messageSource.addBasenames("classpath:messages/datastore-postgis-messages");
        LOG.warn("DataStore Postgis chargé !!!");
    }
}

By using messageSource.addBasenames we load the datastore-postgis-messages.properties stored in the module's src/main/resources/messages folder.

Upvotes: 4

Chris Savory
Chris Savory

Reputation: 2755

Put your messages.properties in your cartocontrib-web module in the src/main/resources/messages folder

Upvotes: 0

Related Questions