springboot如何读取自定义配置项?
网站上总是需要用到一些自定义的配置, 我把它放在一个Properties文件中, 如: config。properties 此文件放在与Struts。xml相同的文件夹内, 在此我的是src下。
1。 新建一个工具类PropertiesUtil, 在其定义一个公共的静态的java。
util。Properties类的对象pps
2。 新建一个监听类WebConfigListener并实现ServletContextListener接口
public void contextInitialized(ServletContextEvent arg0) {
PropertiesUtil。
pps = new Properties();
try{
String path = (getClass()。getClassLoader()。getResource("")。toURI())。getPath();
FileInputStream fis = new FileInputStream(path + "config。
properties");
PropertiesUtil。pps。load(fis);
}catch (Exception e) {
e。printStackTrace();
}
}
3。 在WEB。xml中把WebConfigListener添加上去
com。
phonesale。common。WebConfigListener
4。 在程序中读取配置时使用: PropertiesUtil。pps。getProperty(key);
编辑特别推荐:
Java对话框的字体设置
Java开发守护进程
深入Java核心:JVM中的栈和局部变量。
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有