(一) 通过Properties获取
String i18nFile = "i18n/message_zh_CN.properties";
第一种:
Resource resource = new ClassPathResource(i18nFile);
EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
try {
Properties prop = PropertiesLoaderUtils.loadProperties(encodedResource);
String user_role_admin = prop.getProperty("user_role_admin");
System.err.println(user_role_admin);
} catch (IOException e) {
e.printStackTrace();
}
第二种:
InputStream is = getClass().getClassLoader().getResourceAsStream(i18nFile);
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
Properties properties = new Properties();
try {
properties.load(isr);
String username = properties.getProperty("user_role_admin");
System.err.println(username);
} catch (IOException e) {
e.printStackTrace();
}
(二)在spring配置文件中加载,通过注解获取
<context:property-placeholder
location="classpath*:jdbc.properties,
classpath*:application.properties,
classpath*:sinovalidated.properties" />
@Value("${bank.wx.zoneName}")
private String zoneName;