About reading configuration files , There are also many online materials , But the pertinence is not very strong . Most of them are Copy, And the way is single , Now let me summarize about reading Src, namely class Methods and differences of configuration files .
First, let's define a project , Build a Servlet, Request to go to the background and then turn to this page . It mainly depends on Servlet Code processing part . And then in Src Next, create a configuration file config.properties, Define an attribute name.
The first one is :
Class loading mode , This is a piece of explanation on the Internet , But in the end, it is to use the class loader to load , No other two !
Main code :
try { Properties p = new Properties(); // Configuration file in class Next , namely Src Next p.load(TS.class.getClassLoader().getResourceAsStream("config.properties")); String p1 = p.getProperty("name"); System.out.println(" Loading mode :" + p1);} catch (Exception e) { e.printStackTrace();}
There are different ways to obtain class loaders , But I think we can use this class class That's it . Get the stream of the configuration file for parsing .
The second kind :
My own way to become a bond , Define a configuration file and read the properties directly , This configuration file can be under any package .
Main code :
try { // You can add package name , for example com.config refer to com It's a bag config.properties This configuration file ResourceBundle resbun = ResourceBundle.getBundle("config"); String p1 = resbun.getString("name"); System.out.println(" Bonding mode :" + p1);} catch (Exception e) { e.printStackTrace();}
The third kind of :
stay Web We can all get systematic request object , This object can get some properties of the request , You can also get some information about the system , Through this object, you can get the path information of the system , Then get the configuration file and physical file , Then get the file stream and parse .
But if you use an absolute path , Then you should pay attention to the writing method of path separator , Because in Windows and Linux The way of writing is different . We get the system file separator through the environment attribute, and then assemble .
String realpath = request.getSession().getServletContext().getRealPath("");try { Properties p = new Properties(); // Get the file system separator String spa = System.getProperty("file.separator"); // Get the file through the absolute path and then get the stream File file = new File(realpath + spa + "WEB-INF" + spa +"classes" + spa + "config.properties"); FileInputStream fis = new FileInputStream(file); p.load(fis); String p1 = p.getProperty("name"); System.out.println(" Local mode :" + p1);} catch (Exception e) { e.printStackTrace();}
Through the path assembly information, you can see , This method can also read in WEB-INF Define the configuration file .
A fourth :
I call it attribute mode , First get the environmental information , Then load a configuration file by the system itself .
try { Properties p = new Properties(); ServletContext ctx = request.getSession().getServletContext(); // Get the configuration file flow through the environment variable p.load(ctx.getResourceAsStream("WEB-INF/classes/config.properties")); String p1 = p.getProperty("name"); System.out.println(" Attribute mode :" + p1);} catch (Exception e) { e.printStackTrace();} response.sendRedirect("index.jsp");}
You can see through the code , This method can also read WEB-INF The files under the .
But what is the difference between the above four methods ?
In my opinion , The first two methods rely on class loaders , The latter two did not . And the latter two methods can get WEB-INF Profile under , The first two cannot .
The other biggest difference is , After the first two methods are read, if the configuration file is modified , Call again to read , Cannot get the modified attribute .
We can test , You enter index.jsp after , Click to view the background output . Then modify the configuration file directly on the server , Then click to view the output , It is found that the latter two methods read the modified attributes .
I recommend you to read more about “ class The configuration file properties getClassLoader ” The article