自定义监听器类来加载web.xml中的<context-param>系统属性</context-param> 和xxx.properties文件属性
来源:互联网 发布:单片机mp3 编辑:程序博客网 时间:2024/05/16 07:17
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); } }}
0 0
- 自定义监听器类来加载web.xml中的<context-param>系统属性</context-param> 和xxx.properties文件属性
- 获取web.xml中的context-param和init-param参数
- web.xml配置文件中的<context-param>标签
- context-param在web.xml中的作用
- web.xml之<context-param>、<init-param>
- web.xml;context-param;init-param
- Web.xml解读 <context-param>
- Web.xml配置context-param
- web.xml 配置-context-param
- 获取web.xml中的context-param和init-param定义的值
- servlet中配置文件web.xml中的参数context-param和init-param
- 获取web.xml中的context-param和init-param定义的值
- 【转】获取web.xml中的context-param和init-param定义的值
- 获取web.xml中的context-param和init-param定义的值
- servlet中配置文件web.xml中的参数context-param和init-param区别
- Servlet配置web.xml中的context-param和init-param区别
- 获取web.xml中的context-param和init-param的值
- 获取web.xml中的context-param和init-param定义的值
- 字符串练习
- TableView 代理方法详细解释
- RFC 4302 IP Authentication Header(IP认证首部)(未完待续)
- 你真的了解 MySQL 数据库的运行状况吗?
- java中Date无法获取数据库时分秒的问题
- 自定义监听器类来加载web.xml中的<context-param>系统属性</context-param> 和xxx.properties文件属性
- 【Android】ExpandableListView 默认展开和不可点击
- 敢聊照片隐私,截屏监听
- 为什么要使用GetSafeHwnd()函数
- I2C-资料整理
- java的zip解压
- group群组的使用
- jsonp跨域请求数据实例——手机号码查询
- 深入理解HBase Memstore