• 使用web.xml方式加载Spring时,获取Spring context的两种方式


    使用web.xml方式加载Spring时,获取Spring context的两种方式:

    1、servlet方式加载时:

    【web.xml】

    <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext</param-value>
            </init-param>
    </servlet>

    【jsp/servlet】

    ServletContext context = getServletContext();
      
    XmlWebApplicationContext applicationContext = (XmlWebApplicationContext) context.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet");
    
    DataSource dataSource=(DataSource)applicationContext.getBean("dataSource");
    2、listener方式加载时:
    【web.xml】
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext</param-value>
     </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

    【jsp/servlet】

    ServletContext context = getServletContext();
       
    WebApplicationContext applicationContext  = WebApplicationContextUtils.getWebApplicationContext(context);
       
    DataSource dataSource=(DataSource)applicationContext.getBean("dataSource");

    原文:http://chanson.iteye.com/blog/223263

  • 相关阅读:
    计算fibonacci数(多种方法)
    数组求和(两种方法)
    C语言二级指针(指向指针的指针)
    唯品会海量实时OLAP分析技术升级之路
    hive 调优(一)coding调优
    supsplk 服务器被植入木马 挖矿 cpu使用 700%
    OPTS参数设置
    Yarn 内存分配管理机制及相关参数配置
    hive on tez 任务失败
    hive 调优(三)tez优化
  • 原文地址:https://www.cnblogs.com/azhqiang/p/5036271.html
Copyright © 2020-2023  润新知