spring监听器初始化任务

来源:互联网 发布:slackware 网络配置 编辑:程序博客网 时间:2024/06/03 17:46

在做web项目的时候,经常会遇到这样的情况,项目的一些数据需要在项目启动的时候就初始化,然后再后期方便调用。


这里给出一个小例子


-------------------------------------------------------------------

1,先配置监听器,这个很简单

package cn.itcast.surveypark.listener;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.ServletContext;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.stereotype.Component;import org.springframework.web.context.ServletContextAware;/** * 初始化权限监听器 */@SuppressWarnings("rawtypes")@Component//必须要加注解public class IniRightListener implements ApplicationListener,ServletContextAware{@Resourceprivate RightService rs ;//可以注入service//接受servletContext对象private ServletContext sc;//可以注入ServletContext对象public void onApplicationEvent(ApplicationEvent arg0) {//是否是上下文刷新事件if(arg0 instanceof ContextRefreshedEvent){//初始化任务}}//注入scpublic void setServletContext(ServletContext servletContext) {//System.out.println("注入sc");this.sc = servletContext ;}}

0 0
原创粉丝点击