在编译时使用Spring中的MessageSource读取属性文件

时间:2018-01-11 16:20:14

标签: java spring maven properties-file

我有一个麻烦(对不起我的英语)

我通过MessageSource(org.springframework.context.MessageSource)从文件中读取属性:

public class SomeConstants {

   @Autowired
   private static MessageSource messageSource;
   private static final Locale locale = LocaleContextHolder.getLocale();

   public static final String NULL_MESSAGE = messageSource
       .getMessage( "propertyKey", new Object[]{}, locale );
}

属性文件:

propertyKey = "Some message"

我正在尝试将NULL_MESSAGESomeConstants类传递给@NotNull注释(javax.validation.constraints.NotNull)作为消息参数:

@NotNull( message = NULL_MESSAGE )

当尝试编译时,得到编译错误(我使用Maven构建项目):

[ERROR] NULL_MESSAGE value must be a constant expression

在编译期间可以通过MessageSource设置值,或者我应该通过其他方式解决它?

0 个答案:

没有答案