三个类实现通过spring启动web容器

对于struts2这样的web框架,需要整合spring来管理所有的bean,spring本身也提供了ContextLoaderListener来初始化容器。可以通过扩展ContextLoaderListener来控制spring。




WebxApplicationContext 覆盖spring默认的Configlocations。
package com.you.atlas.webx.context;

import org.springframework.web.context.support.XmlWebApplicationContext;

public class WebxApplicationContext extends XmlWebApplicationContext {

    public final static String WEBX_CONFIGURATION_LOCATION                   = "/WEB-INF/webx.xml";
    public final static String WEBX_COMPONENT_CONFIGURATION_LOCATION_PATTERN = "/WEB-INF/webx-*.xml";

    protected String[] getDefaultConfigLocations() {

        if (getNamespace() != null) {
            return new String[] { WEBX_COMPONENT_CONFIGURATION_LOCATION_PATTERN.replace("*", getNamespace()) };
        } else {
            return new String[] { WEBX_CONFIGURATION_LOCATION };
        }
    }

    
}


WebxContextLoader 负责初始化  WebxApplicationContext

package com.you.atlas.webx.context;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.context.ContextLoader;

public class WebxContextLoader extends ContextLoader {

    @Autowired
    protected Class<?> determineContextClass(ServletContext servletContext) throws ApplicationContextException {
        return getDefaultContextClass();
    }
    
    protected Class<?> getDefaultContextClass() {
        return WebxApplicationContext.class;
    }
}



WebxContextLoaderListener 负责初始化  WebxContextLoader
package com.you.atlas.webx.context;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;

public class WebxContextLoaderListener  extends ContextLoaderListener{

  
    protected ContextLoader createContextLoader() {
        return new WebxContextLoader();
    }
}

猜你喜欢

转载自san-yun.iteye.com/blog/1426905