「Spring 5.3」ResourceBundleMessageSourceでメッセージファイルをUTF-8で読み込むサンプル

環境

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Spring 5.3.21
Java17
Eclipse 2022-06 M2 (4.24.0 M2)
Spring 5.3.21 Java17 Eclipse 2022-06 M2 (4.24.0 M2)
Spring 5.3.21
Java17
Eclipse 2022-06 M2 (4.24.0 M2)

プロパティファイルからメッセージを読み込む手順
1.pom.xmlに依存関係ライブラリを追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.21</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.3.21</version>
</dependency>

2.プロパティファイルの作成(user.properties)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
user.info=出身:{0},氏名:{1}
user.city=東京都
user.name=山田 太郎
user.info=出身:{0},氏名:{1} user.city=東京都 user.name=山田 太郎
user.info=出身:{0},氏名:{1}
user.city=東京都
user.name=山田 太郎

3.アノテーション@Configurationで設定クラスを作成します(AppConfig.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
//設定クラス
@Configuration
public class AppConfig {
@Bean
User user() {
//インスタンスを生成して返す
return new User();
}
@Bean
MessageSource msgSource() {
//メッセージファイルを読み込む
ResourceBundleMessageSource msgSrc = new ResourceBundleMessageSource();
//プロパティファイルを指定
msgSrc.setBasename("user");
//文字コード指定
msgSrc.setDefaultEncoding("UTF-8");
return msgSrc;
}
}
package com.arkgame.study; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; //設定クラス @Configuration public class AppConfig { @Bean User user() { //インスタンスを生成して返す return new User(); } @Bean MessageSource msgSource() { //メッセージファイルを読み込む ResourceBundleMessageSource msgSrc = new ResourceBundleMessageSource(); //プロパティファイルを指定 msgSrc.setBasename("user"); //文字コード指定 msgSrc.setDefaultEncoding("UTF-8"); return msgSrc; } }
package com.arkgame.study;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

//設定クラス
@Configuration
public class AppConfig {

      @Bean
      User user() {
            //インスタンスを生成して返す
            return new User();
      }

      @Bean
      MessageSource msgSource() {
            //メッセージファイルを読み込む
            ResourceBundleMessageSource msgSrc = new ResourceBundleMessageSource();
            //プロパティファイルを指定
            msgSrc.setBasename("user");
            //文字コード指定
            msgSrc.setDefaultEncoding("UTF-8");
            return msgSrc;

      }
}

4.プロパティファイルから設定する値を取得します(User.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.stereotype.Component;
//BeanとしてDIコンテナに登録
@Component
public class User {
@Autowired
MessageSource msgSource;
//プロパティファイルから設定する値「user.city」を取得
MessageSourceResolvable strCity = new DefaultMessageSourceResolvable("user.city");
//プロパティファイルから設定する値「user.name」を取得
MessageSourceResolvable strName = new DefaultMessageSourceResolvable("user.name");
public void getMsg() {
//プロパティファイルに{0}、{1}も設定する値「user.info」取得
String msg = msgSource.getMessage("user.info", new MessageSourceResolvable[] { strCity, strName }, Locale.JAPANESE);
System.out.println(msg);
}
}
package com.arkgame.study; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceResolvable; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.stereotype.Component; //BeanとしてDIコンテナに登録 @Component public class User { @Autowired MessageSource msgSource; //プロパティファイルから設定する値「user.city」を取得 MessageSourceResolvable strCity = new DefaultMessageSourceResolvable("user.city"); //プロパティファイルから設定する値「user.name」を取得 MessageSourceResolvable strName = new DefaultMessageSourceResolvable("user.name"); public void getMsg() { //プロパティファイルに{0}、{1}も設定する値「user.info」取得 String msg = msgSource.getMessage("user.info", new MessageSourceResolvable[] { strCity, strName }, Locale.JAPANESE); System.out.println(msg); } }
package com.arkgame.study;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.stereotype.Component;

//BeanとしてDIコンテナに登録
@Component
public class User {

      @Autowired
      MessageSource msgSource;

      //プロパティファイルから設定する値「user.city」を取得
      MessageSourceResolvable strCity = new DefaultMessageSourceResolvable("user.city");
      
      //プロパティファイルから設定する値「user.name」を取得
      MessageSourceResolvable strName = new DefaultMessageSourceResolvable("user.name");

      public void getMsg() {
            //プロパティファイルに{0}、{1}も設定する値「user.info」取得
            String msg = msgSource.getMessage("user.info", new MessageSourceResolvable[] { strCity, strName }, Locale.JAPANESE);
            System.out.println(msg);
      }

}

5.起動するクラス(UserController.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import org.springframework.beans.BeansException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class UserController {
public static void main(String[] args) {
//DIコンテナを生成
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)) {
//DIコンテナからbeanインスタンスを取得
User user = context.getBean(User.class);
user.getMsg();
context.close();
} catch (BeansException e) {
e.printStackTrace();
}
}
}
package com.arkgame.study; import org.springframework.beans.BeansException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class UserController { public static void main(String[] args) { //DIコンテナを生成 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)) { //DIコンテナからbeanインスタンスを取得 User user = context.getBean(User.class); user.getMsg(); context.close(); } catch (BeansException e) { e.printStackTrace(); } } }
package com.arkgame.study;

import org.springframework.beans.BeansException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class UserController {

      public static void main(String[] args) {
            
            //DIコンテナを生成
            try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)) {
                  
                  //DIコンテナからbeanインスタンスを取得
                  User user = context.getBean(User.class);
                  user.getMsg();
                  
                  context.close();
                  
            } catch (BeansException e) {
                  e.printStackTrace();
            }

      }

}

6.動作確認
プロジェクトを右クリックし「実行(R)」ー>[Javaアプリケーション]を押下します。
コンソール画面に「出身:東京都,氏名:山田 太郎」が表示されます。

Spring Boot CLI

Posted by arkgame