Spring中的WebAppRootListener

来源:互联网 发布:java酒店管理系统 编辑:程序博客网 时间:2024/05/21 23:31

WebAppRootListener

这个listener的作用就是监听web.xml中的配置param-name为webAppRootKey的值:

<context-param>        <param-name>webAppRootKey</param-name>        <param-value>myroot</param-value></context-param>

然后配置一个然后配置一个监听器:

<listener>        <listener-class>             org.springframework.web.util.WebAppRootListener        </listener-class></listener>

这个监听器会在web上下文初始化的时候,cs调用webUtil的对应方法,首先获取根传递进来的servletContext得到物理路径,String path=servletContext.getRealPath("/");  然后找到context-param的webAooRootKey对应的param-value,把param-value的值作为key,上面配置的是"myroot", 接着执行System.setProperty("myroot",path)。这样在web中就可以使用System.getProperty("myroot")来获取系统的绝对路径。

注:

1)如果只配置了监听器,没有配置webAppRootKey, 默认wenAppRootKey对应的param-value的值为webapp.root。

2)上面得到系统路径是Spring的做法,和平时自己采用的方法基本一致,都是写一个监听器,然后得到物理路径后手动放入System中,一般还会在这个监听器中加载配置文件,获取配置文件的值。

0 0
原创粉丝点击