startuplistener.java

来自「《JSP网站开发典型模块与实例精讲》一书光盘源码」· Java 代码 · 共 66 行

JAVA
66
字号
package org.appfuse.webapp.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.appfuse.Constants;
import org.appfuse.util.ServiceFactory;
//import com.aspire.demo.service.TestService;

/**
 * StartupListener class used to initialize Spring bean config.
 *
 *
 */
public class StartupListener extends ContextLoaderListener
    implements ServletContextListener {

    private static final Log log = LogFactory.getLog(StartupListener.class);

    public void contextInitialized(ServletContextEvent event) {
        if (log.isDebugEnabled()) {
            log.debug("initializing context...");
        }

        // call Spring's context ContextLoaderListener to initialize
        // all the context files specified in web.xml
        super.contextInitialized(event);

        ServletContext context = event.getServletContext();

        // Orion starts Servlets before Listeners, so check if the config
        // object already exists
//        Map config = (HashMap) context.getAttribute(Constants.CONFIG);
//
//        if (config == null) {
//            config = new HashMap();
//        }
//        context.setAttribute(Constants.CONFIG, config);
//
        setupContext(context);
    }

    public static void setupContext(ServletContext context) {
        ApplicationContext ctx =
            WebApplicationContextUtils.getRequiredWebApplicationContext(context);

        // Initial ServiceFactory
        ServiceFactory.init(ctx);

        // TestService service = (TestService) ctx.getBean("testService");

        if (log.isDebugEnabled()) {
            log.debug("drop-down initialization complete [OK]");
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?