Java 如何读取批注中的messages.properties
我想使用本地化来本地化Swagger文档。但我只能为注释提供编译时常量。因此,我不知道如何从messages_**.properties提供读取消息并将其提供给注释 消息源:Java 如何读取批注中的messages.properties,java,spring-boot,java-8,Java,Spring Boot,Java 8,我想使用本地化来本地化Swagger文档。但我只能为注释提供编译时常量。因此,我不知道如何从messages_**.properties提供读取消息并将其提供给注释 消息源: @Configuration public class CustomMessageSourceConfig { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messag
@Configuration
public class CustomMessageSourceConfig {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
}
正在从邮件中读取邮件。\u**。属性:
@Component
public class MessagesByLocaleServiceImpl implements MessagesByLocaleService {
@Autowired
private MessageSource messageSource;
@Override
public String getMessage(String id) {
Locale locale = LocaleContextHolder.getLocale();
return StringEscapeUtils.unescapeJava(messageSource.getMessage(id, null, locale));
}
}
下面是我如何阅读Java代码中的消息:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot"))).build()
.apiInfo(apiInfo())
.tags(new Tag("Netmap Mode Service", messageSource.getMessage(MessageCodes.SWAGGER_WINDOWS_ONLY)));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(messageSource.getMessage(MessageCodes.SWAGGER_TITLE))
.description(messageSource.getMessage(MessageCodes.SWAGGER_DESCRIPTION))
.contact(messageSource.getMessage(MessageCodes.SWAGGER_CONTACT)).build();
}
但我如何才能为招摇过市的注释提供这些信息呢
@ApiOperation(value = "Add Netmap mode ", notes = "**I want to read properties here**")
@ApiImplicitParams({
@ApiImplicitParam(value = SwaggerSinglePoint.DESC_MODE_NAME, dataType = CDSwaggerPrimitives.STRING, name = SwaggerSinglePoint.MODE_NAME, paramType = CDSwaggerPrimitives.PARAMA_TYPE_QUERY),
@ApiImplicitParam(value = SwaggerSinglePoint.DESC_MODE_BUFFER_SIZE, dataType = CDSwaggerPrimitives.INETEGER, name = SwaggerSinglePoint.BUFFER, paramType = CDSwaggerPrimitives.PARAMA_TYPE_QUERY)})
@RequestMapping(method = RequestMethod.POST, produces = CDConstants.JSON_RESPONSE_DATA_FORMAT, consumes = CDConstants.JSON_REQUEST_DATA_FORMAT)
@SuppressWarnings({ "squid:S3776", "squid:S1319", "unused" })
public String testController(@RequestBody(required = false) HashMap requestParamMap, HttpServletResponse response,
HttpServletRequest request) {
我想阅读这些注释中的消息。非常感谢您提供任何指导或建议。您可以使用
SPEL
即${}
从application.properties
获取值:
@注释(value=“${request.rementer.mails.cron.expression}”)
注意:-道具名称应为
application.properties
的完整名称。您可以使用SPEL
从application.properties
获取值,即${}
:
@注释(value=“${request.rementer.mails.cron.expression}”)
注意:-道具名称应为
应用程序.properties中的完整名称
最好将文档注释与代码分离(从外部属性文件读取文本,而不是以纯文本形式插入)
像这样使用占位符,而不是
@ApiOperation(value = "Add Netmap mode " ,...)
使用
在“messages_**.properties”文件中,应该有一个键值对
message.addNetMode=Add Netmap mode
还要在类级别的配置中注册属性文件
@PropertySource("classpath:messages_**.properties")
**请注意,某些批注的值可能不受支持。请参阅文档,最好将文档注释与代码分离(从外部属性文件读取文本,而不是以纯文本插入) 像这样使用占位符,而不是
@ApiOperation(value = "Add Netmap mode " ,...)
使用
在“messages_**.properties”文件中,应该有一个键值对
message.addNetMode=Add Netmap mode
还要在类级别的配置中注册属性文件
@PropertySource("classpath:messages_**.properties")
**请注意,某些批注的值可能不受支持。请参阅文档,但我想从消息中读取\u**.properties。这个文件不在我的类路径中。不幸的是,你必须把它放在类路径中,但我有这么多的文件messages\u en.properties,messages\u fr.properties等等。我应该加载哪个文件?是否有一种方法可以基于application.properties中的任何其他属性以类路径加载特定文件。例如,如果我在application.properties中有locale=EN,那么我可以加载消息\u EN.properties。这是您要求的级别之前的级别。您正在谈论根据区域设置在加载的文件之间进行更改。此用法与您加载的文件无关,因为这些文件中的属性键相同。但我想从消息中读取\u**.properties。这个文件不在我的类路径中。不幸的是,你必须把它放在类路径中,但我有这么多的文件messages\u en.properties,messages\u fr.properties等等。我应该加载哪个文件?是否有一种方法可以基于application.properties中的任何其他属性以类路径加载特定文件。例如,如果我在application.properties中有locale=EN,那么我可以加载消息\u EN.properties。这是您要求的级别之前的级别。您正在谈论根据区域设置在加载的文件之间进行更改。这种用法与您加载的文件无关,因为这些文件中的属性键相同。但是我有这么多文件messages\u en.properties、messages\u fr.properties等。我应该加载哪个文件?是否有一种方法可以基于application.properties中的任何其他属性以类路径加载特定文件。例如,如果我在application.properties中有locale=EN,那么我就可以加载messages\u EN.properties。@阿披实克,你是如何做到这一点的?但是我有这么多文件messages\u EN.properties、messages\u fr.properties等等。我应该加载哪个文件?是否有一种方法可以基于application.properties中的任何其他属性以类路径加载特定文件。例如,如果我在application.properties中有locale=EN,那么我就可以加载消息\u EN.properties。@Abhishek你是怎么做到的?