我正在寻找将验证错误消息从 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 最终将如下所示。
关于java - 在 spring 和 springboot 中从 src 代码外部化错误消息(资源包)的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372890/