0

我正在寻找将验证错误消息从 spring 和 spring boot 应用程序中的 src 代码外部化的最佳方法,以避免每次错误消息更改时都进行构建/部署。有没有这样的方法来实现它?

4

2 回答 2

0

您可以在属性文件中维护所有验证错误或成功消息。如果要外化,可以将属性文件放在spring boot jar文件之外。无需将配置放入 jar 中。我在下面提供了代码片段来实现它。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

    @Configuration
    @PropertySources({
      @PropertySource("file:config/other-config.properties"),
      @PropertySource("file:config/app-config.properties")
    })
    public class ExternalConfig {

      @Bean
      public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
      }
    }

在上面的代码中@PropertySource("file:config/app-config.properties"),config 是文件夹或目录的名称,其中包含许多属性文件,例如“app-config.properties”。为了更好地理解,我在下面提供了图像,外部配置文件和 Spring Boot jar 最终将如下所示。

在此处输入图像描述

于 2019-05-30T07:07:10.897 回答
0

默认资源包框架假定您的资源包是资源文件夹中的属性文件。因此,它们作为构建过程的一部分被打包在您的 jar 文件中。

但是,您可以实现自己的ResourceBundle加载:

https://docs.oracle.com/javase/tutorial/i18n/resbundle/control.html

然后,您可以选择使用其他机制,包括使用数据库而不是属性文件(使用 TTL 来缓存特定时间段内的消息)。这样,您甚至不必在消息更改时重新启动应用程序。

于 2019-05-30T07:08:51.747 回答