欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java Spring WEB应用实例化如何实现

时间:2021-03-31 09:13:14|栏目:JAVA代码|点击:

1.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext

AbstractApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");

下面讲解直接通过配置文件进行初始化。

2.web.xml

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:beans.xml</param-value>
</context-param>

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这样,ApplicationContext便已经实例化了,默认就直接加载了beans.xml里面的内容。

来看看底层的代码,类ContextLoaderListener中有个初始化方法

public void contextInitialized(ServletContextEvent event) {
    this.contextLoader = createContextLoader();
    if (this.contextLoader == null) {
      this.contextLoader = this;
    }
    this.contextLoader.initWebApplicationContext(event.getServletContext());
  }

进入initWebApplicationContext方法 :

ApplicationContext parent = loadParentContext(servletContext);
      // Store context in local instance variable, to guarantee that
      // it is available on ServletContext shutdown.
      this.context = createWebApplicationContext(servletContext, parent);

这句也就是容器加载的结果。

1和2一个是java代码一个是xml代码,不过实现的效果都是一样的。

上一篇:java中常见的死锁以及解决方法代码

栏    目:JAVA代码

下一篇:java实现在SSM下使用支付宝扫码支付功能

本文标题:Java Spring WEB应用实例化如何实现

本文地址:http://www.codeinn.net/misctech/91620.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有