java - 在 spring 和 springboot 中从 src 代码外部化错误消息(资源包)的最佳方法是什么?

标签 java resourcebundle externalizable

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

最佳答案

您可以在属性文件中维护所有验证错误或成功消息。如果要外部化,可以将属性文件放在 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是包含许多属性文件的文件夹或目录的名称,例如“应用程序配置.属性”。为了更好地理解,我在下面提供了图像,外部配置文件和 Spring Boot jar 最终将如下所示。

enter image description here

关于java - 在 spring 和 springboot 中从 src 代码外部化错误消息(资源包)的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372890/

相关文章:

netbeans - 如何向现有的 netbeans 项目套件添加新语言?

Java 外部化与 transient

java - 通过 TCP channel 发送大文件并进行序列化

java - 引用从对象数组进行打印的方法时找不到符号错误

android-studio - Android Studio - 在模块中包含 ResourceBundles

java - 为什么 spring-boot 找不到消息包?

java - 当类字段的数据类型更改时处理反序列化

java - jersey http客户端自定义请求方式

java - 如何对具有不同方法名称但相同返回类型的不同对象应用相同的过程

java - response.body().getBasketShopList为空,但Postman中的API JSON不为空