spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
看一下ContextLoaderListener的源码,这是一个ServletContextListener
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
contextLoader的源码
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws BeansException {
long startTime = System.currentTimeMillis();
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
servletContext.log("Loading Spring root WebApplicationContext");
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
WebApplicationContext wac = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
if (logger.isInfoEnabled()) {
logger.info("Using context class [" + wac.getClass().getName() +
"] for root WebApplicationContext");
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext [" + wac +
"] as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return wac;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
注意WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,这里面放了WebApplicationContext,需要使用时从ServletContext取出
可以使用WebApplicationContextUtils得到WebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (attr == null) {
return null;
}
if (attr instanceof RuntimeException) {
throw (RuntimeException) attr;
}
if (attr instanceof Error) {
throw (Error) attr;
}
if (!(attr instanceof WebApplicationContext)) {
throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);
}
return (WebApplicationContext) attr;
}
关键的问题在于struts如何启动的spring的,ContextLoaderPlugIn的源码
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getModulePrefix();
}
不同加载的Key竟然不同,原因就是WebApplicationContext放在那里的问题,可spring调用的时候会根据WebApplicationContext里面定义的那个名字去找的,问题出在这里
在struts-config.xml中配置
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
原理是这样的,Struts虽然只能有一个ActionServlet实例,但是对于不同的子应用分别能有自己的RequestProcessor实例每个RequestProcessor实例分别对应不同的struts配置文件。
子应用的ProcessorClass类必须重写一般就是继承RequestProcessor类,然后再其配置文件的controller元素中的<processorClass>属性中作出修改。那么当
getRequestProcessor(getModuleConfig(request)).process(request,response);就能根据request选择相应的moduleconfig,再根据其<processorClass>属性选择相应的RequestProcessor子类来处理相应的请求了。
分享到:
相关推荐
org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....
如果有多个映射文件需要声明,使用类路径映射方式显然比直接单独指定映射文件名的方式要简便。 第27~30行定义了Spring代理Hibernate数据操作的HibernateTemplate模板,而第32~34行将该模板注入到tfileDAO中。 ...
Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...
ContextLoaderListener ContextLoaderPlugIn ContextLoaderServlet ContextRefreshedEvent ContextSingletonBeanFactoryLocator ControlFlow ControlFlowFactory ControlFlowFactory.Jdk13ControlFlow ...
GUI API包含的类分为三个部分:组件类(component class) 容器类(container class),和辅助类(helper class) 1. 组件类是用来创建用户图形界面的,例如JButton,JLabel,JTextField. 2. 容器类是用来包含其他组件的,例如JFrame,JPanel 3. 辅助类是用来支持GUI组件的,例如Color,Font
汉诺塔c语言递归
mybatis-plus-core.jar 各个版本,免费下载, mybatis-plus-core.jar 是 MyBatis 的增强工具核心包。免费下载 。 MyBatis-Plus(简称 MP),是一个 MyBatis 的增强工具包,只做增强不做改变,为简化开发工作、提高生产率而生。
mybatis-plus-generato.jar 包,各个版本,免费下载。 mybatis-plus 代码生成器生成代码框架。各个版本,免费下载。 下载不了,关注我,评论区联系我。
行业研究
stm32项目知识领域: 网络安全 技术关键词: 漏洞分析、渗透测试、加密算法 内容关键词: 攻击防范、身份验证、网络流量分析 用途: 学习保护信息系统免受恶意攻击和数据泄露 资源描述: "OWASP官方网站"提供了全面的网络安全知识和工具,包括常见漏洞、安全指南和最佳实践。
mybatis-plus-core.jar 各个版本,免费下载, mybatis-plus-core.jar 是 MyBatis 的增强工具核心包。免费下载 。 MyBatis-Plus(简称 MP),是一个 MyBatis 的增强工具包,只做增强不做改变,为简化开发工作、提高生产率而生。
测试YUV图像数据的具体格式(1).zip
多思计算机组成原理实验三 存储器实验.docx
行业分析报告
st-stm32cubeide_1.15.0_20695_20240315_1429_x86_64
调节篮球比赛定时器,毕业设计实验报告,multisim仿真,AD09原理图及PCB图
halcon缺陷检测
JSP 标准标签库(JSP Standard Tag Library,JSTL)是一个实现 Web 应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML 操作以及数据库访问. 如果要使用JSTL,则必须将standard-1.1.2.jar和 jstl-1.2.jar文件放到classpath中
本文深入探讨了微电网多目标调度问题,提出了一种改进的多目标粒子群算法(MOPSO),以实现微电网系统在经济性、可靠性和环境影响等多个目标函数之间的优化平衡。文章详细介绍了算法的改进机制,并通过案例分析展示了其在实际微电网调度中的应用效果。本研究适用于电力系统工程师、能源管理专业人士以及对智能电网和优化算法感兴趣的学者。使用场景包括微电网的规划、运行和维护,特别是在考虑经济性和环境因素时的系统优化。 关键词 微电网
视频批量剪辑,亲测可用,操作简单,视频创作者福音