标签:配置文件信息
1、在tomcat中webapps目录下放置一个config.properties配置文件;
2、在监听的时候读取写入src目录下;
3、通过Properties类再次读取,可以在service层获取到,具体代码如下:
1、监听类,实现ServletContextListener接口
public class InitJDBCListener implements ServletContextListener {
private static final Logger log = Logger.getLogger(InitJDBCListener.class);
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
// autoSetupJDBC(context);
loadJDBCProperties(context);
// 加载项目配置文件
loadConfigProperties(context);
// 此时, web.xml 中的context-param 元素已经被加载到ServletContext 对象中。
setWPSFlag(context);
setJDBCKeys(context);
}
private void loadConfigProperties(ServletContext sc){
log.info("加载项目配置文件");
File newMsgProperties = getNewMsgPropertiesFile(sc);
File oldMsgProperties = getOldMsgPropertiesFile(sc);
try {
setMsgProperties(oldMsgProperties, newMsgProperties);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void setMsgProperties(File oldMsgProperties,File newMsgProperties) throws IOException{
if(newMsgProperties != null && oldMsgProperties != null){
FileInputStream fis = new FileInputStream(newMsgProperties);
FileOutputStream fos = new FileOutputStream(oldMsgProperties);
byte[] bs = new byte[fis.available()];
fis.read(bs);
fos.write(bs);
fos.flush();
fis.close();
fos.close();
}
}
private File getOldMsgPropertiesFile(ServletContext sc) {
String appDir = sc.getRealPath(File.separatorChar + "");
String msgPropertiesPath = appDir + "WEB-INF" + File.separatorChar
+ "classes" + File.separatorChar + "config.properties";
File msgFile = new File(msgPropertiesPath);
if (msgFile.exists())
return msgFile;
return null;
}
private File getNewMsgPropertiesFile(ServletContext sc) {
String appDir = sc.getRealPath(File.separatorChar + "");
File appDirFile = new File(appDir);
String appContainerPath = appDirFile.getParent();
String msgPropertiesPath = appContainerPath + File.separatorChar + "config_gagb" + File.separatorChar
+ "config.properties";
File msgFile = new File(msgPropertiesPath);
if (msgFile.exists())
return msgFile;
return null;
}
}
2、在web.xml中配置监听,代码如下:
<!-- 必须放在ContextLoaderListener之前-->
<listener>
<listener-class>
org.gagb.webapp.listener.InitJDBCListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3、通过Properties 读取配置文件:
/**
* 读取配置文件
* config.properties放在src目录下
* @return
*/
public static Properties getConfigProperties() {
Properties prop = new Properties();
try {
java.net.URL url = CommonUtil.class.getResource("/config.properties");
String savePath = url.getPath();
// 以下方法读取属性文件会缓存问题
// InputStream in = PropertiesUtils.class
// .getResourceAsStream("/config.properties");
InputStream in = new BufferedInputStream(new FileInputStream(savePath));
prop.load(in);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return prop;
}
4、在serve中添加Properties静态变量:
private static Properties pro = CommonUtil.getConfigProperties();
5、在方法中根据key获取value:
String adcode = pro.getProperty(Constants.AD_CODE);
6、如果在struts.xml中配置了<constant name="struts.custom.i18n.resources" value="ApplicationResources,errors,jdbc,gis,axis,msg,config" /> 则可以直接通过getText("key")获取;
本文出自 “画江湖之不良人” 博客,谢绝转载!
标签:配置文件信息
原文地址:http://751756016.blog.51cto.com/6308835/1878956