zoukankan      html  css  js  c++  java
  • 使用web.xml方式加载 Spring时,获取Spring context的两种方式:

    1、servlet方式加载时:

    【web .xml】

    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】

    Java代码

    代码
     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】

    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> 

    Java代码

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


  • 相关阅读:
    Mysql 小技巧
    关于提交form不刷新的问题
    取消超链接点击默认事件
    JS获取地址栏参数
    Maven 手动添加 JAR 包到本地仓库
    Mysql函数instr、locate、position VS like
    阿里巴巴常考面试题及汇总答案
    JS跳转action
    Struts2使用ModelDriven后JSON数据返回不正确
    简单的使用AngularJS的解析JSON
  • 原文地址:https://www.cnblogs.com/zhangxz/p/1825847.html
Copyright © 2011-2022 走看看