Web环境下几种容器与SpringIOC容器

简介: Web环境下几种容器与SpringIOC容器

IOC容器的获取主要思想是通过配置监听器,在项目启动时,查找类路径下的applicationContext.xml文件,创建容器。或者如果项目使用全注解,则使用配置类创建容器。

项目中的容器包含关系 :

ServletContext(Tomcat创建) >
Root WebApplicationContext(IOC Web根容器)>
Servlet WebApplicationContext(SpringMVC 子容器)

如下所示在注册contextLoaderListener时会创建RootAppContext:

【1】自定义监听器获取IOC容器

实现ServletContextListener

如下是获取ClassPathXmlApplicationContext容器,这需要有applicationContext.xml文件。

public class SpringServletContextListener implements ServletContextListener {
  public void contextDestroyed(ServletContextEvent sce) {
    System.out.println("容器销毁********");
  }
  public void contextInitialized(ServletContextEvent sce) {
    ServletContext servletContext = sce.getServletContext();
    //获取配置文件名称
    String configLocation = servletContext.getInitParameter("configLocation");
    //创建IOC容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocation);
    //放入servletContext中
    servletContext.setAttribute("applicationContext", applicationContext);
    if (applicationContext != null) {
      System.out.println("IOC 容器初始化成功");
    }
  }
}

web.xml

<context-param>
    <param-name>configLocation</param-name>
    <param-value>applicationContext.xml</param-value>
</context-param>
  <!-- ******启动IOC容器的ServletContextListener****** -->
  <listener>
    <display-name>SpringServletContextListener</display-name>
    <listener-class>
      com.web.listener.SpringServletContextListener
    </listener-class>
  </listener>

【2】Spring自带监听器进行IOC容器初始化

Spring提供了org.springframework.web.context.ContextLoaderListener在项目启动时,自动初始化容器。同样,需要在web.xml里面配置applicationContext.xml路径;

 <!-- ****Spring IOC 容器初始化**** -->
  <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>

【2】Spring自带监听器进行IOC容器初始化

Spring提供了org.springframework.web.context.ContextLoaderListener在项目启动时,自动初始化容器。同样,需要在web.xml里面配置applicationContext.xml路径;

 <!-- ****Spring IOC 容器初始化**** -->
  <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>

【3】获取IOC容器的两种方式

public void contextInitialized(ServletContextEvent sce) {
  ServletContext servletContext = sce.getServletContext();
/*第一种取得applicationContext的方式,根据属性名获取*/
  ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  /*查看源码
  ContextLoader.class的public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
  servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
  }可知,在创建applicationContext时,将其根据属性名放入了servletContext中
  */
  /*第二种取得applicationContext的方式,使用Spring提供的工具类,传入应用上下文对象,就可以获取IOC容器*/
  applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  /*如果在jsp页面获取,则传入application隐式对象*/
}

【3】IOC工具类

package com.corn.core.spring;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.FrameworkServlet;
import com.corn.core.utils.ToolUtils;
public class SpringIocUtil {
  private final static  Logger log = LoggerFactory.getLogger(SpringIocUtil.class);
  public  final static  String fail_info = "get %s  fail,ServletContext  not loading.";
  public  final static  String springConfigFilePath = "applicationContext.xml";
  public  final static  String success_info = "get %s   success:";
  public static synchronized <T> T getBean(Class<T> beanName) {
    return getInstence(beanName, getContext());
  }
  public static synchronized <T> T getBean(Class<T> beanName,ApplicationContext context) {
    return getInstence(beanName, context);
  }
  public static synchronized <T> T getBean(Class<T> beanName,ServletContext servletcontext) {
    return getInstence(beanName, WebApplicationContextUtils.getRequiredWebApplicationContext(servletcontext));
  }
  public static synchronized Object getBean(String beanName) {
    return getInstence(beanName, getContext());
  }
  public static synchronized Object getBean(String beanName,ApplicationContext context) {
    return getInstence(beanName, context);
  }
  public static synchronized Object getBean(String beanName,ServletContext servletcontext) {
    return getInstence(beanName, WebApplicationContextUtils.getRequiredWebApplicationContext(servletcontext));
  }
  private static  <T> T getInstence(Class<T> beanName,ApplicationContext context) {
    try {
      if (context == null) {
        log.error(String.format(fail_info, beanName));
        return null;
      }
      T t = context.getBean(beanName);
      if (t != null) {
        log.debug(String.format(success_info, beanName) + t);
        return t;
      }
    } catch (Exception e) {
      log.error(SpringIocUtil.class.getName()+" T getInstence(beanName,context) ",e);
    }
    return null;
  }
  private static  Object getInstence(String beanName,ApplicationContext context) {
    try {
      if (context == null) {
        log.error(String.format(fail_info, beanName));
        return null;
      }
      Object t = context.getBean(beanName);
      if (t != null) {
        log.debug(String.format(success_info, beanName) + t);
        return t;
      }
    } catch (Exception e) {
      log.error(SpringIocUtil.class.getName()+" Object getInstence(beanName,context)",e);
    }
    return null;
  }
  public static ApplicationContext getContext() {
    ApplicationContext context = null;
    try{
      Enumeration<String> ns= SpringIocContextListener.getServletcontext().getAttributeNames();
      while(ns.hasMoreElements()){
        String name=ns.nextElement();
        log.debug(name+" IS SERVLET_CONTEXT_PREFIX:",name.matches(FrameworkServlet.SERVLET_CONTEXT_PREFIX+"*"));
      }
      context = WebApplicationContextUtils.getWebApplicationContext(SpringIocContextListener.getServletcontext());
    }catch(Exception e){
      log.warn("Spring IOC getWebApplicationContext(sc) type fail.");
    }
    String name=null;
    if (context == null) {
      try{
        name=FrameworkServlet.SERVLET_CONTEXT_PREFIX+"Spring-MVC";
        context=WebApplicationContextUtils.getWebApplicationContext(SpringIocContextListener.getServletcontext(), name);
      }catch(Exception e){
        log.warn("Spring IOC getWebApplicationContext(sc,\""+name+"\") type fail[default].");
      }
    }
    if (context == null) {
      try{
        Enumeration<String> ns_= SpringIocContextListener.getServletcontext().getAttributeNames();
        while(ns_.hasMoreElements()){
           name=ns_.nextElement();//从spring 配置中获取上下面名称
          if(null!=name&&name.trim().length()>0&&name.matches(FrameworkServlet.SERVLET_CONTEXT_PREFIX+"*")){
            context=WebApplicationContextUtils.getWebApplicationContext(SpringIocContextListener.getServletcontext(), name);
            break;
          }
        }
      }catch(Exception e){
        log.warn("Spring IOC getWebApplicationContext(sc,\""+name+"\") type fail.");
      }
    }
    if (context == null) {
      try{
        context=WebApplicationContextUtils.getRequiredWebApplicationContext(SpringIocContextListener.getServletcontext());
      }catch(Exception e){
        log.warn("Spring IOC getRequiredWebApplicationContext type fail.");
      }
    }
    if (context == null) {
      try{
          context = initLocalAppContext();
      }catch(Exception e){
        e.printStackTrace();
        log.warn("Spring IOC ClassPathXmlApplicationContext type fail.");
      }
    }
    return context;
  }
  private static ApplicationContext initLocalAppContext() {
    if(ToolUtils.isEmpty(springConfigFilePath)){
      log.error("Spring Config FilePath is empty.");
      return null;
    }
    ApplicationContext context = new ClassPathXmlApplicationContext(springConfigFilePath);
    log.info("load " + springConfigFilePath + " success!");
    return context;
  }
}


目录
相关文章
|
2月前
|
Kubernetes 供应链 安全
云原生环境下的容器安全与最佳实践
云原生时代,容器与 Kubernetes 成为企业应用核心基础设施,但安全挑战日益突出。本文探讨容器安全现状与对策,涵盖镜像安全、运行时防护、编排系统风险及供应链安全,提出最小权限、漏洞扫描、网络控制等最佳实践,并结合阿里云 ACK、ACR 等服务提供全链路解决方案,展望零信任、AI 安全与 DevSecOps 融合趋势。
93 4
|
3月前
|
运维 数据可视化 C++
2025 热门的 Web 化容器部署工具对比:Portainer VS Websoft9
2025年热门Web化容器部署工具对比:Portainer与Websoft9。Portainer以轻量可视化管理见长,适合技术团队运维;Websoft9则提供一站式应用部署与容器管理,内置丰富开源模板,降低中小企业部署门槛。两者各有优势,助力企业提升容器化效率。
300 1
2025 热门的 Web 化容器部署工具对比:Portainer VS Websoft9
|
3月前
|
缓存 Ubuntu Docker
Ubuntu环境下删除Docker镜像与容器、配置静态IP地址教程。
如果遇见问题或者想回滚改动, 可以重启系统.
207 16
|
4月前
|
存储 缓存 Serverless
【Azure Container App】如何在Consumption类型的容器应用环境中缓存Docker镜像
在 Azure 容器应用的 Consumption 模式下,容器每次启动均需重新拉取镜像,导致冷启动延迟。本文分析该机制,并提出优化方案:使用 ACR 区域复制加速镜像拉取、优化镜像体积、设置最小副本数减少冷启动频率,或切换至 Dedicated 模式实现镜像缓存,以提升容器启动效率和应用响应速度。
|
6月前
|
Kubernetes Cloud Native 区块链
Arista cEOS 4.30.10M - 针对云原生环境设计的容器化网络操作系统
Arista cEOS 4.30.10M - 针对云原生环境设计的容器化网络操作系统
181 0
|
11月前
|
负载均衡 网络协议 算法
Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式
本文探讨了Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式,以及软件负载均衡器、云服务负载均衡、容器编排工具等实现手段,强调两者结合的重要性及面临挑战的应对措施。
341 3
|
12月前
|
开发者 Docker Python
从零开始:使用Docker容器化你的Python Web应用
从零开始:使用Docker容器化你的Python Web应用
456 4
|
12月前
|
机器学习/深度学习 数据采集 Docker
Docker容器化实战:构建并部署一个简单的Web应用
Docker容器化实战:构建并部署一个简单的Web应用
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo

热门文章

最新文章

下一篇
开通oss服务