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");


  • 相关阅读:
    hdu 3342 Legal or Not 拓排序
    hdu 1596 find the safest road Dijkstra
    hdu 1874 畅通工程续 Dijkstra
    poj 2676 sudoku dfs
    poj 2251 BFS
    poj Prime Path BFS
    poj 3278 BFS
    poj 2387 Dijkstra 模板
    poj 3083 DFS 和BFS
    poj 1062 昂贵的聘礼 dijkstra
  • 原文地址:https://www.cnblogs.com/zhangxz/p/1825847.html
Copyright © 2011-2022 走看看