1, in Tomcat in the WebApps directory to place a config.properties configuration file;
2, in the listening time read write to the SRC directory;
3, through the properties class again read, can be obtained at the service layer, the specific code is as follows:
1, listening class, implement Servletcontextlistener interface
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);
Load project configuration file
Loadconfigproperties (context);
At this point, the Context-param element in Web. XML has been loaded into the ServletContext object.
Setwpsflag (context);
Setjdbckeys (context);
}
private void Loadconfigproperties (ServletContext SC) {
Log.info ("Load project configuration file");
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, configure the monitoring in Web. XML, the code is as follows:
<!--must be placed before Contextloaderlistener--
<listener>
<listener-class>
Org.gagb.webapp.listener.InitJDBCListener
</listener-class>
</listener>
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3. Read the configuration file via properties:
/**
* Read configuration file
* Config.properties placed in src directory
* @return
*/
public static Properties Getconfigproperties () {
Properties prop = new properties ();
try {
Java.net.URL URL = CommonUtil.class.getResource ("/config.properties");
String Savepath = Url.getpath ();
The following method reads the properties file to cache the problem
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. Add the properties static variable in serve:
private static Properties Pro = Commonutil.getconfigproperties ();
5, in the method according to key to get value:
String Adcode = Pro.getproperty (Constants.ad_code);
6. If <constant name= "struts.custom.i18n.resources" value= "Applicationresources,errors,jdbc,gis" is configured in Struts.xml, Axis,msg,config "/> can be obtained directly through GetText (" key ");
This article from "Painting The Lake of Bad People" blog, declined to reprint!
Read external configuration file information in struts