1、ResourceBundle读取.properties 配置文件
例如,存在一个Messages.properties 配置文件,根据key读取value过程如下:
@SuppressWarnings("serial")
public abstract class DispatcherServlet extends HttpServlet {
private ResourceBundle messagesResource;
private static final String MESSAGEBASENAME = "Messages";
/**
* 将通过此部分读取资源文件
*/
@Override
public void getMessagesResource() throws ServletException {
Locale locale = Locale.getDefault();// 根据当前语言环境取得语言编码
this.messagesResource = ResourceBundle.getBundle(MESSAGEBASENAME, locale);
}
this.messagesResource.getString(msgKey);//获取properties 配置文件中key值对应的value
}
2、Properties读取.properties 配置文件
// 继承的是Hashtable,也是根据key读取value
FileInputStream fileInputStream=new FileInputStream("Test.properties");
Properties properties=new Properties();
properties.load(Messages.properties);
fileInputStream.close();
//获取properties 配置文件中key值对应的value
properties.getProperty("key");
//修改properties 配置文件中key值对应的value
properties.setProperty("key","value");
3、 spring中利用ApplicationContext 加载xml文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Computer computer = (Computer) applicationContext.getBean("computer");
computer.run();