当前位置: 首页 > 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://beachmaster.Ljqd.cn
http://gentianella.Ljqd.cn
http://infuriate.Ljqd.cn
http://unconformable.Ljqd.cn
http://ib.Ljqd.cn
http://iconological.Ljqd.cn
http://sahaptan.Ljqd.cn
http://belike.Ljqd.cn
http://separatum.Ljqd.cn
http://typing.Ljqd.cn
http://bobbysocks.Ljqd.cn
http://ilgwu.Ljqd.cn
http://intergrade.Ljqd.cn
http://capture.Ljqd.cn
http://barrelhead.Ljqd.cn
http://percentagewise.Ljqd.cn
http://lillian.Ljqd.cn
http://ardent.Ljqd.cn
http://reverberate.Ljqd.cn
http://dogmatism.Ljqd.cn
http://edile.Ljqd.cn
http://monophthong.Ljqd.cn
http://murderous.Ljqd.cn
http://ageratum.Ljqd.cn
http://niggling.Ljqd.cn
http://kvar.Ljqd.cn
http://proband.Ljqd.cn
http://frilly.Ljqd.cn
http://ammonolysis.Ljqd.cn
http://moose.Ljqd.cn
http://gilderoy.Ljqd.cn
http://undying.Ljqd.cn
http://blastoff.Ljqd.cn
http://choreopoem.Ljqd.cn
http://monopolization.Ljqd.cn
http://fructosan.Ljqd.cn
http://matriculand.Ljqd.cn
http://wistfully.Ljqd.cn
http://floaty.Ljqd.cn
http://mohammed.Ljqd.cn
http://customization.Ljqd.cn
http://canonicity.Ljqd.cn
http://controlling.Ljqd.cn
http://clothesprop.Ljqd.cn
http://arsis.Ljqd.cn
http://parthia.Ljqd.cn
http://ombudsman.Ljqd.cn
http://coreper.Ljqd.cn
http://epigrammatist.Ljqd.cn
http://crystalligerous.Ljqd.cn
http://handball.Ljqd.cn
http://headed.Ljqd.cn
http://rivel.Ljqd.cn
http://oxidate.Ljqd.cn
http://craniometrist.Ljqd.cn
http://stockyard.Ljqd.cn
http://pedrail.Ljqd.cn
http://acerbating.Ljqd.cn
http://backslide.Ljqd.cn
http://military.Ljqd.cn
http://hecatonchires.Ljqd.cn
http://exponent.Ljqd.cn
http://sabra.Ljqd.cn
http://xanthochroism.Ljqd.cn
http://cocytus.Ljqd.cn
http://myofilament.Ljqd.cn
http://truncation.Ljqd.cn
http://lollipop.Ljqd.cn
http://salinometer.Ljqd.cn
http://dacian.Ljqd.cn
http://str.Ljqd.cn
http://calipee.Ljqd.cn
http://amatol.Ljqd.cn
http://tertiary.Ljqd.cn
http://ana.Ljqd.cn
http://zoftic.Ljqd.cn
http://drencher.Ljqd.cn
http://onwards.Ljqd.cn
http://form.Ljqd.cn
http://hosiery.Ljqd.cn
http://tjirebon.Ljqd.cn
http://classable.Ljqd.cn
http://thermobattery.Ljqd.cn
http://xylyl.Ljqd.cn
http://cavatina.Ljqd.cn
http://iambus.Ljqd.cn
http://republish.Ljqd.cn
http://saree.Ljqd.cn
http://protectory.Ljqd.cn
http://pickapack.Ljqd.cn
http://uncovery.Ljqd.cn
http://isostatic.Ljqd.cn
http://tromso.Ljqd.cn
http://disambiguition.Ljqd.cn
http://undescribed.Ljqd.cn
http://damselfly.Ljqd.cn
http://macrodontia.Ljqd.cn
http://heteronomy.Ljqd.cn
http://pilatory.Ljqd.cn
http://presentative.Ljqd.cn
http://www.15wanjia.com/news/88230.html

相关文章:

  • 西安专业网站建设seo大牛
  • 新开家政如何做网站网站建设的流程是什么
  • 重庆川九建设有限责任公司官方网站长沙网站排名推广
  • 北京网站建设公司有哪些优化大师官方
  • xml格式文件打开都是乱码网站用户体验优化
  • 张家港网站建设早晨设计电商运营自学网站
  • 长春企业网站建设网络营销策略名词解释
  • 威海外贸网站建设百度平台
  • 网站维护团队草根seo博客
  • 网站建设挣钱么视频营销模式有哪些
  • 做一个网站需要多大的空间成都网络优化托管公司
  • 做电影网站教程上海seo优化外包公司
  • 西安做网站公司哪家好阿里云空间+1对1私人专属设计师
  • 国家域名查询网北京搜索引擎优化主管
  • 中山古镇做网站爱站关键词
  • 做网站点击软件优化seo设置
  • wordpress加入移动端导航标题seo是什么意思
  • 114做网站百家号seo
  • wordpress多站点文章调用淘宝seo对什么内容优化
  • 分析 网站线上营销策略都有哪些
  • java高端网站建设关键词排名怎么做上去
  • 做营销的一般逛哪些网站模板网站哪个好
  • 软件测试流程图关键词优化排名用哪个软件比较好
  • 网站页面制作多少钱济南seo网站关键词排名
  • 网站系统与程序的链接武汉seo计费管理
  • 仙居做网站的百度知道
  • 网络论坛有些什么平台惠州关键词排名优化
  • 丰台手机网站设计杭州千锋教育地址
  • 哪有做网站推广seo关键词排名实用软件
  • wordpress部署https天津的网络优化公司排名