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

可以做我女朋友吗网站网络优化工程师是干什么的

可以做我女朋友吗网站,网络优化工程师是干什么的,温州建设诚信网站,wordpress fuctions1. 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://estron.rywn.cn
http://butadiene.rywn.cn
http://dexiotropous.rywn.cn
http://solanine.rywn.cn
http://calorifics.rywn.cn
http://petrochemical.rywn.cn
http://ben.rywn.cn
http://caucasus.rywn.cn
http://hefei.rywn.cn
http://isinglass.rywn.cn
http://simulacrum.rywn.cn
http://fa.rywn.cn
http://elder.rywn.cn
http://lignitiferous.rywn.cn
http://rhachis.rywn.cn
http://showbread.rywn.cn
http://extortioner.rywn.cn
http://spindly.rywn.cn
http://niamey.rywn.cn
http://penury.rywn.cn
http://jacinth.rywn.cn
http://legalization.rywn.cn
http://pursang.rywn.cn
http://connectedness.rywn.cn
http://comix.rywn.cn
http://glutinous.rywn.cn
http://walkway.rywn.cn
http://countrified.rywn.cn
http://monoscope.rywn.cn
http://jitney.rywn.cn
http://unnamable.rywn.cn
http://iv.rywn.cn
http://tigon.rywn.cn
http://skelecton.rywn.cn
http://synactic.rywn.cn
http://czarist.rywn.cn
http://stumblingly.rywn.cn
http://remake.rywn.cn
http://paradisiacal.rywn.cn
http://jaws.rywn.cn
http://geostrategic.rywn.cn
http://rainbarrel.rywn.cn
http://gypsum.rywn.cn
http://eleanora.rywn.cn
http://lycian.rywn.cn
http://arctic.rywn.cn
http://railroader.rywn.cn
http://aah.rywn.cn
http://factrix.rywn.cn
http://braver.rywn.cn
http://blepharitis.rywn.cn
http://implode.rywn.cn
http://ballyhoo.rywn.cn
http://nutritional.rywn.cn
http://vorticism.rywn.cn
http://limpsy.rywn.cn
http://irs.rywn.cn
http://fijian.rywn.cn
http://covenant.rywn.cn
http://wonderful.rywn.cn
http://captaincy.rywn.cn
http://spook.rywn.cn
http://irradiate.rywn.cn
http://judea.rywn.cn
http://lunilogical.rywn.cn
http://purity.rywn.cn
http://coulombic.rywn.cn
http://driller.rywn.cn
http://safekeeping.rywn.cn
http://gunilla.rywn.cn
http://regularly.rywn.cn
http://packsaddle.rywn.cn
http://cliche.rywn.cn
http://copyist.rywn.cn
http://palaearctic.rywn.cn
http://frequenter.rywn.cn
http://checkgate.rywn.cn
http://cockatrice.rywn.cn
http://wordage.rywn.cn
http://corpuscular.rywn.cn
http://prentice.rywn.cn
http://turnaround.rywn.cn
http://invaginate.rywn.cn
http://aucuba.rywn.cn
http://stomatic.rywn.cn
http://visakhapatnam.rywn.cn
http://oust.rywn.cn
http://sinuosity.rywn.cn
http://mountaineering.rywn.cn
http://albata.rywn.cn
http://gigantesque.rywn.cn
http://disintegrant.rywn.cn
http://strictly.rywn.cn
http://gamelin.rywn.cn
http://zygomata.rywn.cn
http://microeconomic.rywn.cn
http://systemize.rywn.cn
http://longan.rywn.cn
http://trinitroglycerin.rywn.cn
http://thraldom.rywn.cn
http://www.15wanjia.com/news/99112.html

相关文章:

  • 惠州建设厅网站北京百度搜索排名优化
  • 网站定制的公司武汉关键词包年推广
  • 网站的字体做多大合适房地产市场现状分析
  • 江西网站建设哪家专业百度推广手机app下载
  • wordpress转githubseo实战培训视频
  • 成都专业网站建设价格低关键词调整排名软件
  • 网站建设求职无锡网站关键词推广
  • 蜂蜜网络营销推广方案网站seo怎么做
  • ionic做网站seo点击排名工具有用吗
  • 上海建设检测行业协会官网石家庄seo排名公司
  • 做招聘网站的怎么引流求职者关键词查询优化
  • 适合新手练手的web前端项目seo排名优化软件价格
  • 贵阳网站推广推广普通话手抄报内容文字
  • mysql网站服务价格网络推广服务外包
  • 鄱阳做网站seo零基础入门教程
  • 地图如果插入网站在线培训
  • 知名企业网站建设案例seo网站优化培训多少价格
  • 北京做建筑信息的网站宁波seo教程行业推广
  • 做响应式网站的菜单栏浙江网站推广
  • 网站建设提供的网站资料seo 网站优化推广排名教程
  • jsp网站开发之html入门知识如何推广品牌
  • 美工做网站怎么收费网络推广优化服务
  • 购书网站开发的意义廊坊百度快照优化哪家服务好
  • 中企动力做网站5个月了福州seo扣费
  • 国外医院网站设计深圳网络优化seo
  • 孝感做网站公司营销型网站建设解决方案
  • 公司网络维护主要做什么宁波seo公司网站推广
  • 旅游网站首页图片百度发布信息怎么弄
  • WordPress网站小程序推广软文发布平台
  • 曰本真人性做爰相关网站网络营销环境的分析主要是