需求说明做项目时,为了省事,起初把初始化的配置都放在每个类中 static加载,初始化配置一多,就想把它给整理一下,这里使用servlet中的init方法初始化。web.xml说明首先了解下WEB.xml中元素的加载顺序: 启动web项目
需求说明
做项目时,为了省事,起初把初始化的配置都放在每个类中 static加载,初始化配置一多,就想把它给整理一下,这里使用servlet中的init方法初始化。
web.xml说明
首先了解下WEB.xml中元素的加载顺序:
完整加载顺序:ServletContext -> context-param -> listener-> filter -> servlet
配置实现
InitServlet.java:
public class InitServlet extends httpservlet { private static final long serialVersionUID = 1L; @Override public void init(){ try { if(Constants.PAY_URL.size()==0){ List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl(); for(CommonEntity entity:listPayUrl){ Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName()); } } LogUtil.info("佛祖保佑 永不宕机 永无BUG :初始化系统数据数量:"+Constants.PAY_URL.size()); Configs.init("zfbinfo.properties"); LogUtil.info("初始化支付宝配置信息"); SDKConfig.getConfig().loadPropertiesFromSrc(); LogUtil.info("初始化银联支付配置信息"); } catch (Exception e) { e.printStackTrace(); } } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Constants.PAY_URL = new ConcurrentHashMap<String, String>(); List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl(); for(CommonEntity entity:listPayUrl){ Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName()); } LogUtil.info("初始化系统数据数量:"+Constants.PAY_URL.size()); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
--结束END--
本文标题: JavaWeb中web.xml初始化加载顺序详解
本文链接: https://www.lsjlt.com/news/226372.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0