zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    Hyper-V无法启动虚拟机因为虚拟机监控程序未运行
    SpringBoot项目中自动加载datasourceConfig配置导致启动失败
    redis 数据类型与命令
    Redis入门与安装,与配置
    MySQL 主从配置
    MySql 中的事务
    什么是Docker?
    window10下安装Docker
    Docker 常见命令
    原生SQL语句
  • 原文地址:https://www.cnblogs.com/azhqiang/p/5036271.html
Copyright © 2011-2022 走看看