首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Web器皿启动后执行代码的几种方式

2013-05-02 
Web容器启动后执行代码的几种方式1.实现Servlet监听器接口ServletContextListenerpackage com.yli.initim

Web容器启动后执行代码的几种方式

1.实现Servlet监听器接口ServletContextListener

package com.yli.init;import java.util.List;import java.util.Map;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.springframework.web.context.WebApplicationContext;import com.suning.teach.service.ShopService;public class InitListener implements ServletContextListener {    @Override    public void contextDestroyed(ServletContextEvent context) {        // 上下文销毁之后执行    }    @Override    public void contextInitialized(ServletContextEvent context) {        // 上下文初始化执行        System.out.println("================>[ServletContextListener]自动加载启动开始.");        // 读取Spring容器中的Bean[此时Bean已加载,可以使用]        WebApplicationContext wac = WebContextUtil.getContext();        if (wac != null) {            ShopService shopService = wac.getBean("shopService", ShopService.class);            System.out.println("================>shopService[" + shopService + "]");            List<Map<String, Object>> shopList = shopService.findAllShop();            System.out.println("================>" + shopList);        }        System.out.println("================>[ServletContextListener]自动加载启动结束.");    }}

?

然后在web.xml文件配置该监听器

?

<listener><listener-class>com.yli.init.InitListener</listener-class></listener>

?

?

注意:从Spring IOC容器中读取Bean的工具类WebContextUtil

package com.yli.init;import org.springframework.web.context.ContextLoader;import org.springframework.web.context.WebApplicationContext;public class WebContextUtil {        private static WebApplicationContext wac;    public static WebApplicationContext getContext(){        if(null == wac){            wac = ContextLoader.getCurrentWebApplicationContext();        }        return wac;    }}

?

2.实现Servlet的过滤器Filter

package com.yli.init;import java.io.IOException;import java.util.List;import java.util.Map;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.springframework.web.context.WebApplicationContext;import com.suning.teach.service.ShopService;public class InitFilter implements Filter {    @Override    public void destroy() {    }    @Override    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException,            ServletException {    }    @Override    public void init(FilterConfig config) throws ServletException {        System.out.println("================>[Filter]自动加载启动开始.");        // 读取Spring容器中的Bean[此时Bean已加载,可以使用]        WebApplicationContext wac = WebContextUtil.getContext();        if (wac != null) {            ShopService shopService = wac.getBean("shopService", ShopService.class);            System.out.println("================>shopService[" + shopService + "]");            List<Map<String, Object>> shopList = shopService.findAllShop();            System.out.println("================>" + shopList);        }        System.out.println("================>[Filter]自动加载启动结束.");    }}

?

然后在web.xml文件配置过滤器即可

<filter><filter-name>InitFilter</filter-name><filter-class>com.yli.init.InitFilter</filter-class></filter><filter-mapping><filter-name>InitFilter</filter-name><url-pattern>/</url-pattern></filter-mapping>

?

?

3.编写一个Servlet,在web.xml里面配置容器启动后执行即可

import java.util.List;import java.util.Map;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import org.springframework.web.context.WebApplicationContext;import com.suning.teach.service.ShopService;public class InitServlet extends HttpServlet {    /**     */    private static final long serialVersionUID = 1L;    @Override    public void init(ServletConfig config) {        try {            super.init();        } catch (ServletException e) {            e.printStackTrace();        }        System.out.println("================>[Servlet]自动加载启动开始.");        // 读取Spring容器中的Bean[此时Bean已加载,可以使用]        WebApplicationContext wac = WebContextUtil.getContext();        if (wac != null) {            ShopService shopService = wac.getBean("shopService", ShopService.class);            System.out.println("================>shopService[" + shopService + "]");            List<Map<String, Object>> shopList = shopService.findAllShop();            System.out.println("================>" + shopList);        }        System.out.println("================>[Servlet]自动加载启动结束.");    }}

?

然后在web.xml文件配置该Servlet的启动方式为:容器启动后执行

<servlet><servlet-name>InitServlet</servlet-name><servlet-class>com.yli.init.InitServlet</servlet-class><init-param><param-name>username</param-name><param-value>yli</param-value></init-param>                <!-- 此处指定加载顺序为2,表明还有优先级更高的Servlet要先执行 --><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>InitServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

?关于启动后执行,由load-on-startup指定:

(1)当值为0或者大于0时,表示容器在应用启动时就加载这个servlet。值越小,启动优先级越高;

(2)当是一个负数时或者没有指定时,表示该servlet被调用时才加载。

?

4.如果你使用Spring IOC作为Bean管理容器,那么可以指定init-method

其中init-method表示bean加载成功后,立即执行某个方法。配置如下:start为要执行的方法名称

<!-- service --><bean id="shopService" init-method="start"><property name="shopDao" ref="shopDao" /></bean><!-- dao --><bean id="shopDao" ref="jdbcTemplate" /></bean>

?

Java代码如下

package com.yli.teach.service;import java.util.List;import java.util.Map;import com.yli.teach.dao.ShopDao;public class ShopService{    private ShopDao shopDao;    // set 注入    public void setShopDao(ShopDao shopDao) {        this.shopDao = shopDao;    }        /**     *      * 〈容器启动后执行方法〉     */    public void start(){        System.out.println("================>[init-method]自动加载启动开始.");        List<Map<String, Object>> shopList = shopDao.findAllShop();        System.out.println("================>" + shopList);        System.out.println("================>[init-method]自动加载启动结束.");    }} 

?

5.如果你使用Spring IOC作为Bean管理容器,还可以实现Spring的Bean后置处理器接口:BeanFactoryPostProcessor

表示:该Bean加载完成之后,Spring可以让开发者自定义一些事件,Java代码如下

package com.yli.init;import java.util.List;import java.util.Map;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import com.suning.teach.service.ShopService;public class KeyWordInit implements BeanFactoryPostProcessor {    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {        System.out.println("================>[BeanFactoryPostProcessor]自动加载启动开始.");        ShopService shopService = factory.getBean("shopService", ShopService.class);        List<Map<String, Object>> shopList = shopService.findAllShop();        System.out.println("================>" + shopList);        System.out.println("================>[BeanFactoryPostProcessor]自动加载启动结束.");    }}

?

这个Bean在xml文件做正常配置即可,如下

<bean id="keyWordInit" class="com.yli.init.KeyWordInit"></bean>

?

=================关于以上5种方式代码执行顺序的说明===================

4===>5===>1===>2===>3

即指定init-method的Bean开始执行

接着实现Spring的Bean后置处理器最先开始执行

然后是Servlet的监听器执行

再接下来是Servlet的过滤器执行

最后才是Servlet执行

?

热点排行