Springbootでは、国際化ファイル(messages.properties)を構成して列挙クラス(enum)に値を割り当てます.
12270 ワード
1.アプリケーション.propertiesで国際化ファイルを構成するパス spring.messages.basename=i18n/messages
2.列挙クラスjava package com.example.d3.enums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.EnumSet;
public enum Level {
LOW("0", "level.LOW"),
MEDIUM("1", "level.MEDIUM"),
HIGH("2", "level.HIGH");
private String value;
private String description;
private Level(String value, String description) {
this.value = value;
this.description = description;
}
public String getValue() {
return this.value;
}
public String getDescription() {
return messageSource.getMessage(description, null, description, null);
}
private MessageSource messageSource;
public Level setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
return this;
}
@Component
public static class EnumValuesInjectionService {
@Autowired
private MessageSource messageSource;
// bean, 。
@PostConstruct
public void postConstruct() {
for (Level level : EnumSet.allOf(Level.class)) {
level.setMessageSource(messageSource);
}
}
}
}
3.メッセージでpropertiesにテスト情報を追加 level.LOW=
level.MEDIUM=
level.HIGH=
4.テストクラスの作成 package com.example.d3;
import com.example.d3.enums.Level;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class EnumTest {
private static final Logger logger = LoggerFactory.getLogger(EnumTest.class);
@Test
public void test() {
for (Level level : Level.values()) {
String value = level.getValue();
String description = level.getDescription();
logger.info(value + " ==> " + description);
}
}
}
5.実行および出力結果 2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 0 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 1 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 2 ==>
spring.messages.basename=i18n/messages
package com.example.d3.enums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.EnumSet;
public enum Level {
LOW("0", "level.LOW"),
MEDIUM("1", "level.MEDIUM"),
HIGH("2", "level.HIGH");
private String value;
private String description;
private Level(String value, String description) {
this.value = value;
this.description = description;
}
public String getValue() {
return this.value;
}
public String getDescription() {
return messageSource.getMessage(description, null, description, null);
}
private MessageSource messageSource;
public Level setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
return this;
}
@Component
public static class EnumValuesInjectionService {
@Autowired
private MessageSource messageSource;
// bean, 。
@PostConstruct
public void postConstruct() {
for (Level level : EnumSet.allOf(Level.class)) {
level.setMessageSource(messageSource);
}
}
}
}
3.メッセージでpropertiesにテスト情報を追加 level.LOW=
level.MEDIUM=
level.HIGH=
4.テストクラスの作成 package com.example.d3;
import com.example.d3.enums.Level;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class EnumTest {
private static final Logger logger = LoggerFactory.getLogger(EnumTest.class);
@Test
public void test() {
for (Level level : Level.values()) {
String value = level.getValue();
String description = level.getDescription();
logger.info(value + " ==> " + description);
}
}
}
5.実行および出力結果 2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 0 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 1 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 2 ==>
level.LOW=
level.MEDIUM=
level.HIGH=
package com.example.d3;
import com.example.d3.enums.Level;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class EnumTest {
private static final Logger logger = LoggerFactory.getLogger(EnumTest.class);
@Test
public void test() {
for (Level level : Level.values()) {
String value = level.getValue();
String description = level.getDescription();
logger.info(value + " ==> " + description);
}
}
}
5.実行および出力結果 2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 0 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 1 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 2 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 0 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 1 ==>
2020-01-16 20:57:33.334 INFO 45386 --- [ main] com.example.d3.EnumTest : 2 ==>