当前位置: 首页 > news >正文

兴义做网站的公司住房和城乡建设部网站注册进度

兴义做网站的公司,住房和城乡建设部网站注册进度,wordpress关闭评论框,网站建设语言都有什么一、自行创建监听器来模拟springweb架构中的监听器流程 监听器的作用:当web服务器开始启动时,使用监听器监听初始化周期,并将spring配置文件放置在Servlet的最大域中。一旦服务器启动后就可以直接从域中使用gertAtribute方法去获取到spring的…

一、自行创建监听器来模拟springweb架构中的监听器流程

监听器的作用:当web服务器开始启动时,使用监听器监听初始化周期,并将spring配置文件放置在Servlet的最大域中。一旦服务器启动后就可以直接从域中使用gertAtribute方法去获取到spring的配置文件applicationContext.xml。

然后会设置一个工具类,通过工具类作为中介来返回配置文件信息。

自行配置逻辑应是如下:

首先是Controller层

public class UserController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {//调用applicationContext配置文件首先通过工具类WebApplicationContextUtils//再通过该类中的getApplicationContext从最大域中获得监听器上传的getApplicationContext配置文件内容//调用顺序为:WebApplicationContextUtils  ->  getApplicationContext// ->  (ApplicationContext) servletContext.getAttribute("applicationContext")ApplicationContext applicationContext =WebApplicationContextUtils.getApplicationContext(req.getServletContext());userService = (UserService) applicationContext.getBean("userService");userService.save();}

然后是web.xml配置文件

<web-app><display-name>Archetype Created Web Application</display-name>
  <context-param><param-name>contextConfigLocation</param-name><param-value>applicationContext.xml</param-value></context-param>
<!--  监听器配置--><listener><listener-class>listener.ContextLoaderListener</listener-class></listener>
  <servlet><servlet-name>UserController</servlet-name><servlet-class>com.hxy.usercontroller.UserController</servlet-class></servlet><servlet-mapping><servlet-name>UserController</servlet-name><url-pattern>/userController</url-pattern></servlet-mapping>
</web-app>

监听器类的创建

public class ContextLoaderListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {ServletContext servletContext = sce.getServletContext();String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");sce.getServletContext().setAttribute("applicationContext", contextConfigLocation);System.out.println("ContextLoaderListener contextInitialized");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {}
}
工具类的创建
//设置一个工具,用于返回spring配置类:applicationContext
//外部只需要调用这个类的方法getApplicationContext,就能够得到返回的applicationContext
public class WebApplicationContextUtils {public static ApplicationContext getApplicationContext(ServletContext servletContext) {return (ApplicationContext) servletContext.getAttribute("applicationContext");}
}

二、使用spring框架中的监听器直接创建项目

只需将web.xml中的监听器配置文件改为spring-web框架自带的,同时在Controller层获取配置文件时调用spring框架自带的工具类即可,省去了创建监听器以及工具类的过程

三、总结

使用监听器的作用是简化提取配置文件的过程,方便操作。

监听器的作用在于监听web服务器的初始化过程,将配置文件进行传递,从而方便后续的获取。

通过监听器的模式,实现了解耦,如果修改了配置文件的名称或内容,将不必再在代码中重复修改,这个过程只需要在web.xml配置文件中进行相应修改即可。

http://www.dtcms.com/a/517398.html

相关文章:

  • 深圳网站设计公司招聘麻涌企业网站建设
  • 鸿蒙Next音频开发新篇章:深入解析Audio Kit(音频服务)
  • UVa 10712 Count the Numbers
  • 网站建设免费加盟代理网站建设案例知名 乐云践新
  • 【矩阵分析与应用】【第8章 特征分析】【8.3 凯莱-哈密顿定理(Cayley-Hamilton)及其应用】
  • 做英文小说网站wordpress前端切换风格
  • python---生成器
  • 动易会提示模版文件"默认网站首页问答模板.html"找不到做兼职的网站都有哪些
  • Xshell自用指南
  • 抽学号v6.6.6功能预设
  • 自助建站广告发布昆山网站设计
  • 沈阳设计网站公司坂田网站设计
  • 修复shell中复制粘贴中文乱码多#问题
  • 网站黑链 工具阿里巴巴运营的工作内容
  • 淘宝网站建设 深圳网站建设与维护的论述题
  • MySQL 开源主从复制实战指南(SRE 可靠性优先版)
  • Zephyr Custom Board Guide
  • 一页网站首页图如何做蛋糕公司网页设计模板
  • 网站建设这块是怎么挣钱抖音app下载
  • 中国建设银行快捷付授权网站双模网站开发
  • 网站建设推广代理商.概述网站建设的基本流程
  • android studio-设置android模拟器屏幕自动旋转
  • Linux复习(1)
  • 做网站高手武冈企业建站
  • xml解释 OA表单配置文件的权限信息
  • 临海网站制作好了如何上线一个网页前端和后端
  • 兰州网站建设最新招聘信息做特效很牛的一个外国网站
  • 零基础入门C语言之操作符详解1
  • 元隆盛建设集团有限公司网站青海企业网站建设开发
  • 福建网站建建设房地产销售流程详细