1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

messageについて

Last updated at Posted at 2024-03-28
@Configuration
public class AppConfig {

    @Autowired
    private MessageSource messageSource;

    @Bean
    public String getMessage() {
        // プレースホルダーを置換する値の配列
        Object[] args = {"John", "Doe"};
        
        // ロケールを取得
        Locale locale = LocaleContextHolder.getLocale();

        // MessageSourceResolvableを使用してメッセージを取得
        MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("hello.message", args, "Default message", locale);
        String message = messageSource.getMessage(resolvable, locale);
        
        return message;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:message");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}
@Service
public class LoggingService {

    @Autowired
    private MessageSource messageSource;

    public void logHelloMessage() {
        String helloMessage = messageSource.getMessage("logging.message.hello", null, null);
        System.out.println(helloMessage);
    }

    public void logErrorMessage(String error) {
        String errorMessage = messageSource.getMessage("logging.message.error", new Object[]{error}, null);
        System.out.println(errorMessage);
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?