web项目初始化操作

前言:现在java web一般会用到Spring容器,那么当我们需要在项目初始化,进行一系列操作时该怎么办?很常见的一种情况就是,项目启动时要进行数据库一系列的操作。

在讲解具体操作之前先补充一下基础知识:
①web容器初始化的步骤是
web.xml的初始化顺序:context-param –> listener –> filter –> servlet 。

org.springframework.web.context.ContextLoaderListener  


当web.xml配置如上代码时则会启动Spring容器, ContextLoaderListener该类实现了ServletContextListener

③ ServletContextListener能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。

当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。在 ServletContextListener 接口中定义了处理ServletContextEvent 事件的两个方法。

④当 Web 应用集成 Spring 容器后,代表 Spring 容器的WebApplicationContext对象将以
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键存放在ServletContext的属性列表中。您当然可以直接通过以下语句获取 WebApplicationContext:
WebApplicationContext wac = (WebApplicationContext)servletContext.
getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
但通过位于 org.springframework.web.context.support 包中的WebApplicationContextUtils 工具类获取 WebApplicationContext 更方便:
WebApplicationContext wac =WebApplicationContextUtils.
getWebApplicationContext(servletContext);

那么若要在web启动的时候做一些操作,那么要怎么做?

1.方案 实现Spring BeanPostProcessor类

public class CacheBeanPostProcessor implements BeanPostProcessor{
    DataCenters dataCenter = new DataCenters();

    /* Bean实例化完毕后执行的后处理操作,所有初始化逻辑、装配逻辑之前执行,
     * 如果返回false将阻止其他的InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation的执行,
     * (3.2和(9.1处执行;在此处可以执行一些初始化逻辑或依赖装配逻辑;
     * */
    public Object postProcessAfterInitialization(Object obj, String arg1)
            throws BeansException {
            if(obj instanceof SqlSessionTemplate){
                Firm.setSession((SqlSessionTemplate) obj);
                dataCenter.init();
            }
        return obj;
    }

    public Object postProcessBeforeInitialization(Object arg0, String arg1)
            throws BeansException {

        return arg0;
    }

}

可以看出Spring在加载bean的时候都会回调postProcessAfterInitialization该方法,那么上图就是mybatis的SqlSessionTemplate实例化了后即可获取SqlSessionTemplate的实例进行一些数据库操作了。

2.继承 上面基础知识提到的ContextLoaderListener

package com.ysb.framework.listener;

import javax.servlet.ServletContextEvent;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class BeanUtil extends ContextLoaderListener {


private static ApplicationContext applicationContext;

public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
//这里即可进行自己的初始化操作
applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
}

public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
将spring加载配置的监听重写一下,重写内容也很简单,继承一下ContextLoaderListener ,然后创建一个spring的静态容器,需要用到spring容器里的bean时就从静态容器里面取就行了。

web.xml中的spring的加载监听改为如下。


com.ysb.framework.listener.BeanUtil


楼主的加载方式是用的servlet,其实监听和servlet都是一样,目的就是在系统启动时执行那段加载代码。

3.继承ApplicationObjectSupport

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;

public final class ToolSpring extends ApplicationObjectSupport {

    private static ApplicationContext applicationContext = null;

    @Override
    protected void initApplicationContext(ApplicationContext context)

    throws BeansException {

        super.initApplicationContext(context);
        //这里进行初始化操作
        if (ToolSpring.applicationContext == null) {
            ToolSpring.applicationContext = context;
        }
    }

    public static ApplicationContext getAppContext() {

        return applicationContext;

    }

    public static Object getBean(String name) {

        return getAppContext().getBean(name);

    }

}

未完,待续。

你可能感兴趣的:(spring)