未加载多模块 spring 引导项目 messages.properties

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

我用这种结构开发了一个模块化的 Spring Boot 2 应用程序:

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

模块 "cartocontrib-datastore-postgis" 是一个 jar,必须定义一些 JSP,并且 messages.properties

我设法将模块的 JSP 放入模块的 "src/main/resources/META-INF/resources" 文件夹中。

但是我收到模块 messages.properties 中定义的消息的错误:

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

应用程序似乎没有加载 jar 模块的 messages.properties。

有人可以帮我让它工作吗?

将您的 messages.properties 放入 src/main/resources/messages 文件夹中的 cartocontrib-web 模块

终于找到怎么做了!

第一步:在主 webapp 中定义一个 CustomMessageSourceConfiguration class:

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;
    }
}

所以它会在 src/main/resources/messages 文件夹中加载 messages.properties。

就像这里解释的那样:https://www.baeldung.com/spring-custom-validation-message-source

第二步:在模块配置中使用@PostConstruct 添加自定义 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é !!!");
    }
}

通过使用 messageSource.addBasenames 我们加载存储在模块的 src/main/resources/messages 文件夹中的 datastore-postgis-messages.properties。