1:配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>webpage/index.html</welcome-file>
<welcome-file>webpage/index.htm</welcome-file>
<welcome-file>webpage/index.jsp</welcome-file>
<welcome-file>webpage/default.html</welcome-file>
<welcome-file>webpage/default.htm</welcome-file>
<welcome-file>webpage/default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>USERNAME</param-name>
<param-value>liufukin</param-value>
</context-param>
<listener>
<listener-class>system.InitListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/springAnnotation-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2:創建自定義監聽器類InitListener來加載web.xml中的系統屬性 和xxx.properties文件屬性
package system;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import util.LoadProperties;
/**
* @author Kin.Liufu
* @created 2016-1-25
* @describe
*/
public class InitListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web exit ... ");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("system init .......");
//加載web.xml中的配置信息
System.out.println(sce.getServletContext().getInitParameter("USERNAME"));
//加載.properties配置文件的信息,但是需要LoadProperties這個靜態類的支持
System.out.println(LoadProperties.getProperty("config.properties", "userPwd", "liufu"));
}
}
3:加載xxx.properties屬性文件需要LoadProperties的支持,所以就自己寫了一個加載properties文件的工具類
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Properties;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class LoadProperties {
private static Logger loger = Logger.getLogger(LoadProperties.class);
private static Hashtable<String, Properties> htProp = new Hashtable<String, Properties>();
public static String getProperty(String properties, String key)
throws IOException {
Properties p = getProperties(properties);
return p.getProperty(key)!=null ? p.getProperty(key).trim() : null;
}
public static Properties getProperties(String properties)
throws IOException {
loger.info("start to read the properties file");
Properties p = (Properties) htProp.get(properties);
if (p == null) {
LoadProperties.retrieveKeyMap(properties);
p = (Properties) htProp.get(properties);
if (p == null) {
throw new IOException("Can't retrieve specified properties: "
+ properties);
}
}
loger.info("end to read the properties file");
return p;
}
public static String getProperty(String properties, String key, String defaultValue)
{
try{
Properties p = getProperties(properties);
return p.getProperty(key,defaultValue);
}
catch(Exception e)
{
}
return defaultValue;
}
public static String getProperty(String properties, String key,
Locale locale) throws IOException {
String langName = locale.toString();
return getProperty(properties, key + "_" + langName);
}
public static void refresh(String properties) throws IOException {
retrieveKeyMap(properties);
}
private static void retrieveKeyMap(String propertyFileName) throws IOException {
Properties p = new Properties();
InputStream is = LoadProperties.class.getResourceAsStream("/" + propertyFileName);
if (is == null) {
is = LoadProperties.class
.getResourceAsStream(propertyFileName);
if (is == null) {
is = ClassLoader.getSystemResourceAsStream(
propertyFileName);
}
}
if (is == null) {
loger.log(Level.INFO, "Resource file cannot be found: " + propertyFileName);
} else {
p.load(is); // throw IOException
is.close();
htProp.put(propertyFileName, p);
}
}
}