对于Message Source实现的每个调用,我想在调用远程服务器之前进行缓存。
这是伪代码。
@Configuration
public class ConfigurationForMessageSource {
@Bean
public MessageSource messageSource() {
return new AbstractMessageSource() {
@Autowired
RemoteMessageSource remoteMessageSource;
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
String message = remoteMessageSource.translate(code);
return createMessageFormat(message, locale);
}
};
}
}
服务实施
@Service
public class RemoteMessageSource {
@Cacheable(value = "message", key = "#code")
public String translate(String code) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return dataBase.get(code);
}
}
github上提供了完整的实现:spring-boot-cache-messagesource