`
y806839048
  • 浏览: 1107525 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

webapplicationcontext在spring中的生成

阅读更多

 

 servletcontext这里有配置文件的参数等信息,还有bean之间的关联关系,并且把这种关联关系封装成map(把webapplicationcontext放入)

 

 

 

 

 

srping  webapplicationcontext

 

 

ContextLoaderListener:

 

 

@Override

public void contextInitialized(ServletContextEvent event) {

initWebApplicationContext(event.getServletContext());

}

 

 

 

 

 

 

//生成webapplicationcontext并放入servletcontext

 

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {

if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {

throw new IllegalStateException(

"Cannot initialize context because there is already a root application context present - " +

"check whether you have multiple ContextLoader* definitions in your web.xml!");

}

 

Log logger = LogFactory.getLog(ContextLoader.class);

servletContext.log("Initializing Spring root WebApplicationContext");

if (logger.isInfoEnabled()) {

logger.info("Root WebApplicationContext: initialization started");

}

long startTime = System.currentTimeMillis();

 

try {

// Store context in local instance variable, to guarantee that

// it is available on ServletContext shutdown.

if (this.context == null) {

this.context = createWebApplicationContext(servletContext);

}

if (this.context instanceof ConfigurableWebApplicationContext) {

ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;

if (!cwac.isActive()) {

// The context has not yet been refreshed -> provide services such as

// setting the parent context, setting the application context id, etc

if (cwac.getParent() == null) {

// The context instance was injected without an explicit parent ->

// determine parent for root web application context, if any.

ApplicationContext parent = loadParentContext(servletContext);

cwac.setParent(parent);

}

configureAndRefreshWebApplicationContext(cwac, servletContext);

}

}

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

 

ClassLoader ccl = Thread.currentThread().getContextClassLoader();

if (ccl == ContextLoader.class.getClassLoader()) {

currentContext = this.context;

}

else if (ccl != null) {

currentContextPerThread.put(ccl, this.context);

}

 

if (logger.isDebugEnabled()) {

logger.debug("Published root WebApplicationContext 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 this.context;

}

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;

}

}

 

 

 

 

 

 

  • 大小: 360.4 KB
  • 大小: 109.5 KB
  • 大小: 134.1 KB
分享到:
评论

相关推荐

    Spring MVC 4.2.4.RELEASE 中文文档

    DispatcherServlet 的处理流程则解释了请求在 Spring MVC 中的处理过程,包括请求的接收、处理以及响应的生成。 控制器的实现部分通过使用@Controller 注解定义了一个控制器类,并通过@RequestMapping 注解映射请求...

    Spring2.5-中文参考手册chm.zip

    10. **工具集**:Spring 2.5包含了一个强大的工具集,如Schema工具,用于生成基于XML配置的Java源代码,以及Bean工厂工具,用于在运行时检查和操作Bean。 这个"Spring2.5-中文参考手册chm.zip"文件将帮助开发者深入...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring TestContext

    Spring TestContext Framework是spring-test模块中提供的一个测试框架,能够用于单元测试和集成测试。

    Spring面试专题及答案.pdf

    - 能够在实例化对象的同时生成对象之间的协作关系,从而将bean自身与其客户端的配置解耦。 - **ApplicationContext**: - 继承自`BeanFactory`,提供了更高级别的服务,如国际化支持、事件发布等。 - 在实际开发...

    Spring IOC源码解读

    此外,Spring的IOC容器还支持对集合类型的注入,如List、Map等,以及对工厂方法的调用,允许动态生成Bean实例。在处理复杂依赖关系时,Spring还可以通过自动装配(Auto-Wiring)来简化配置,自动查找并注入匹配的...

    Spring系列面试题129道(附答案解析)

    在Spring中可以通过在XML配置文件中添加context命名空间并声明注解驱动元素,或者使用Java配置类上的注解(如@ComponentScan)来启动注解装配。 27、@Component,@Controller,@Repository,@Service有何区别? 这些...

    struts+habernate+spring整合实例源代码

    Struts、Hibernate和Spring是Java开发中非常流行的三大框架,它们各自在Web应用程序的不同层面上发挥作用,共同构建了一个强大的企业级应用解决方案。Struts主要负责表现层,Hibernate处理持久层,而Spring则作为...

    02-01-07-用30个类高仿真提炼纯手写Spring框架V2.0之IOC与DI1

    在初始化(init)方法中,它会创建IOC容器,通常是WebApplicationContext。这个上下文是ApplicationContext的一个子类,专门为Web应用设计,它可以处理来自HTTP请求的映射和转发。 DispatcherServlet继承自...

    05-Spring MVC面试题.docx

    Spring MVC框架详解 Spring MVC是基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web...WebApplicationContext是Spring MVC的核心组件之一,它提供了一个ApplicationContext的实现,用于管理Web应用程序的资源。

    spring+hibernate+osworkflow

    spring+hibernate+osworkflow,服务器为Tomcat6.0,数据库为Mysql 把viewlivegraph2.jsp改为viewlivegraph.jsp. 其中把wf的获得修改为 ApplicationContext cxt = WebApplicationContextUtils.get...

    spring chm文档

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    将Tapestry整合到Spring里去.doc

    在本文档中,我们将探讨如何将Tapestry与Spring框架集成,从而实现更高效的应用程序开发。Tapestry是一款强大的Java web应用程序框架,专注于组件化和动态页面生成,而Spring则是一个全面的企业级应用框架,提供了...

    Spring的监听器ContextLoaderListener的作用

    在 SSH 项目中,我们可以在 web.xml 文件中添加一段配置:<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> ...

    spring 2.5 reference

    Spring 2.5改进了WebApplicationContext,提供了更强大的Web应用环境支持,使得在Web环境中使用Spring更加顺滑。 9. **Spring表达式语言(Spring Expression Language, SpEL)** SpEL是一种强大的表达式语言,...

    spring笔记

    Spring的IoC容器在默认情况下,会以单例模式返回对象,只有在调用`getBean`方法时才会实例化对象。通过这种方式,Spring实现了对象的依赖管理和生命周期管理,提高了代码的可测试性和可维护性。

    Spring在代码中获取bean的几种方式详解

    "Spring在代码中获取bean的几种方式详解" Spring框架是Java应用程序中最流行的框架之一,它提供了许多功能强大且灵活的功能之一就是Bean管理机制。Bean是Spring框架的核心组件,用于管理应用程序中的业务逻辑。在...

    深入剖析Spring Web源码

    - 通过`WebApplicationContext`,`DispatcherServlet`可以访问到所有的Bean实例,包括自定义的Bean和Spring提供的基础设施Bean。 - **3.1.4 其他Servlet** - Spring Web MVC框架中还包括其他类型的Servlet,如`...

Global site tag (gtag.js) - Google Analytics