码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA获取Spring上下文

时间:2015-04-16 11:55:18      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

1. 添加监听

public class SpringContextListener implements ServletContextListener {
    //获取spring注入的bean对象  
    public static WebApplicationContext springContext; 
    public void contextDestroyed(ServletContextEvent event) {
        //springContext = null;
    }
    /**
     * 获取spring上下文
     */
    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    }
}

2. 在web.xml中配置监听

<listener>  
     <listener-class>com.test.util.SpringContextListener</listener-class>
</listener>

3. 使用

获取ServletContext
SpringContextListener.springContext.getServletContext()
获取webapp路径
String webPath = SpringContextListener.springContext.getServletContext().getRealPath("");

    获取到的路径 /usr/local/tomcat/webapps/multimedia

    multimedia 是我的项目名称

 

JAVA获取Spring上下文

标签:

原文地址:http://www.cnblogs.com/liuchao102/p/4431539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!