首页 > 编程语言 > 详细

JAVA获取Spring上下文

时间:2015-04-16 11:52:06      阅读:330      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!