JAVA配置文件之web.xml

来源:互联网 发布:淘宝卡通图片大全 编辑:程序博客网 时间:2024/04/28 23:18

1. IntrospectorCleanupListener
spring中的提供了一个名为org.springframework.web.util.IntrospectorCleanupListener的监听器。
它主要负责处理由JavaBeans Introspector的使用而引起的缓冲泄露。
spring中对它的描述如下:
它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.web.xml中注册这个listener.可以保证在web应用关闭的时候释放与掉这个web,应用相关的class loader和由它管理的类。如果你使用了JavaBeans Introspector来分析应用中的类,Introspector 缓冲中会保留这些类的引用。
结果在你的应用关闭的时候,这些类以及web 应用相关的class loader没有被垃圾回收.不幸的是,清除Introspector的唯一方式是刷新整个缓冲.这是因为我们没法判断哪些是属于你的应用的引用.所以删除被缓冲的introspection会导致把这台电脑上的所有应用的introspection都删掉.
需要注意的是,spring 托管的bean不需要使用这个监听器.因为spring它自己的introspection所使用的缓冲在分析完一个类之后会被马上从javaBeans Introspector缓冲中清除掉.应用程序中的类从来不直接使用JavaBeans Introspector.所以他们一般不会导致内部查看资源泄露.但是一些类库和框架往往会产生这个问题.
例如:Struts 和Quartz.单个的内部查看泄漏会导致整个的web应用的类加载器不能进行垃圾回收.在web应用关闭之后,你会看到此应用的所有静态类资源(例如单例).这个错误当然不是由这个类自 身引起的.用法很简单,就是在web.xml中加入:
<listener>    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener>
只知道servlet标准不允许在web容器内自行做线程管理,quartz的问题确实存在。对于Web容器来说,最忌讳应用程序私自启动线程,自行进行线程调度,像Quartz这种在web容器内部默认就自己启动了10线程进行异步job调度的框架本身就是很危险的事情,很容易造成servlet线程资源回收不掉,所以我一向排斥使用quartz。quartz还有一个问题就是不支持cluster。导致使用quartz的应用都没有办法做群集。
IntrospectorCleanupListener为啥一定要作为第一个Listener配置?
IntrospectorCleanupListener有这么一句注释:This listener should be registered as the first one in web.xml,* before any application listeners such as Spring's ContextLoaderListener.* This allows the listener to take full effect at the right time of the lifecycle.
做为第一个Listener配置,无非就是让这行代码在所有别的框架启动前运行,也就是在ContextLoaderListener前面执行。
2. ContextLoaderListener为启动Web容器时,自动装配ApplicationContext的配置信息。
在web.xml里面配置listener如下:
<listener>    <listener-class>com.package.Listener.XXXContextListener</listener-class></listener>
XXXContextListener是从ContextLoaderListener 继承而来:
public class XXXContextListener extends ContextLoaderListener {    @Override    public void contextInitialized(ServletContextEvent servletContextEvent) {        super.contextInitialized(servletContextEvent);    }}
public void contextInitialized(ServletContextEvent event) {    this.initWebApplicationContext(event.getServletContext());}
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {    if(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {        throw new IllegalStateException("Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!");    } else {        Log logger = LogFactory.getLog(ContextLoader.class);        servletContext.log("Initializing Spring root WebApplicationContext");        if(logger.isInfoEnabled()) {            logger.info("Root WebApplicationContext: initialization started");        }
3. DispatcherServlet前置控制器
Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步,目标Controller需要在配置文件里面配置dispatcher的配置文件。
<context:component-scan base-package="com.ctrip.application.controller"/>
com.ctrip.application.controller文件夹下面就是对应的Controller,拦截到的请求如果和controller里面的一致就会执行。
<servlet>    <servlet-name>mvc-dispatcher</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup></servlet><servlet-mapping>    <servlet-name>mvc-dispatcher</servlet-name>    <url-pattern>/</url-pattern></servlet-mapping>
4. servlet-mapping定义servlet的映射关系
web.xml中会定义多个servlet,拿到请求之后,对应为哪个servlet去处理,可以用mapping指定。先定义好相关的多个servlet。在下面定义mapping规则:请求包含api字母则对应APIServlet。
<servlet-mapping>    <servlet-name>APIServlet</servlet-name>    <url-pattern>/api/*</url-pattern></servlet-mapping>
5. contextConfigLocation节点表示spring的配置文件
<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext.xml</param-value></context-param>

多个文件以逗号隔开:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>        /WEB-INF/applicationContext-1.xml,        /WEB-INF/applicationContext-2.xml,        /WEB-INF/applicationContext-3.xml    </param-value></context-param>

以上写法可以优化为:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath*:**/applicationContext-*.xml</param-value></context-param>


classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找;

**/表示的是任意目录;

**/applicationcontext-*.xml表示任意目录下的以"applicationcontext-"开头的xml文件。 


等应用部署到tomat服务器上后,

apache-tomcat-7.0.63\webapps\应用名\WEB-INF

文件夹下面会有相应的配置文件:
web.xml
applicationContext.xml
服务器会根据这个配置文件进行spring bean的载入。


SSI框架下的配置方法:

struts2  1+n个       路径:src+src(可配置)        名称: struts.xml  + n
spring   1个           路径: src                           名称: applicationcontext.xml
ibatis     1+n个      路径: src+src(可配置)        名称: sqlmapconfig.xml + n


0 0
原创粉丝点击