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

正规的家居行业网站开发什么网站可以做医疗设备的

正规的家居行业网站开发,什么网站可以做医疗设备的,便宜,开发公司起名1. ApplicationContext应用上下文获取方式 应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多…

1. ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次

在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
web.xml配置全局初始化参数

<!--全局初始化参数--><context-param><param-name>contextConfigLocation</param-name><param-value>applicationContext.xml</param-value></context-param><listener><listener-class>com.zhxd.listener.ContextLoaderListener</listener-class>
</listener>
  • 创建ServletContextListener
package com.zhxd.listener;import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;public class ContextLoaderListener implements ServletContextListener {public void contextInitialized(ServletContextEvent servletContextEvent) {ServletContext servletContext = servletContextEvent.getServletContext();//ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");//隐藏Spring配置文件String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);servletContext.setAttribute("app",app);System.out.println("容器创建完毕...");}public void contextDestroyed(ServletContextEvent servletContextEvent) {}
}
  • UserServlet.java
public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");UserService userService = app.getBean(UserService.class);userService.save();}
}

优化上述程序,把上下文变量名字(app)隐藏:

  • 定义获取上下文对象工具类
package com.zhxd.listener;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;public class WebApplicationContextUtils {public static ApplicationContext getApp(ServletContext servletContext) {return (ApplicationContext) servletContext.getAttribute("app");}
}
  • 修改UserServlet.java
public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();ApplicationContext app = WebApplicationContextUtils.getApp(servletContext);UserService userService = app.getBean(UserService.class);userService.save();}
}

2.Spring提供获取应用上下文的工具

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。

所以我们需要做的只有两件事:
① 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)

<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.5.RELEASE</version>
</dependency>

② 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
配置web.xml

 <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
package com.zhxd.web;import com.zhxd.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);UserService userService = app.getBean(UserService.class);userService.save();}
}
http://www.yayakq.cn/news/136686/

相关文章:

  • 南京站建设wordpress dux 主题
  • 空间网站链接怎么做seo大牛
  • 网站建设的想法做网站规划
  • 网站开发 介绍 回扣网站建设外包需要注意哪些
  • 建设网站广州市试用期工作总结
  • 在公司网站建设会议上的汇报百度竞价广告的位置
  • wordpress 京东主题营口网站优化
  • 网站个人和公司有什么区别是什么企业微信网站开发公司
  • 17网站一起做网店质量怎么样小程序登录不上什么原因
  • 网站移动端推广wordpress外国人留言
  • 淮南公司网站建设网络推广怎么做好
  • 电商网站的制作流程软件系统开发在哪儿
  • 导购类网站备案六间房
  • 广州10大网站服务品牌国内互联网推广
  • 亚马逊站外deal网站桂林 网站建设
  • 网站大部分都是jsp做的副业做网站程序
  • 汶上手机网站建设四方网架公司
  • 凤岗镇网站建设做欧洲电商看哪个网站吗
  • 某些网站字号设置样式网站会员管理系统
  • 张掖交通建设投资有限责任公司网站泰安建设网
  • 网站制作免费四川鸿业建设集团网站
  • 旅行社网站建设规划书天津自动网站建设调试
  • 网站备案号含义吉林省公司注册网站
  • 网站怎么做关键词库金属行业网站模板下载
  • 上海网站建设定做团购网站商品从哪里找
  • 目前热门的网站建设语言潍坊有哪些网站
  • 珠海市官网网站建设品牌线下推广app赚佣金
  • 网站备案ip查询网站查询商务网站的建设步骤
  • 网站备案要网站做才可以使用吗在线做数据图的网站
  • 苏州做网站公司电话酷炫网站推荐