Spring,SpringMvc初始化监听配置

本文介绍SpringMVC中如何通过监听器解决bean容器初始化顺序问题,确保正确初始化及避免空指针异常。通过实现ApplicationListener接口并在springmvc.xml中配置监听器,可实现在容器初始化完成后执行特定任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要是解决spring初始化过程中由于bean容器初始化顺序不同导致注入异常

在springmvc.xml中通过bean注入监听器

<context:component-scan base-package="cn.com.sinosoft.controller"></context:component-scan>
<!-- 初始化完成之后执行监听	 -->
<bean class="cn.com.sinosoft.lisener.IniterListener"/>

监听实现类实现ApplicationListenerpplicationListener接口触发ContextRefreshedEvent事件,重写onApplicationEvent方法

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import cn.com.sinosoft.controller.QuartzManager;
import cn.com.sinosoft.po.Vote;
import cn.com.sinosoft.quartz.job.VoteJob;
import cn.com.sinosoft.service.FxActivityService;
import cn.com.sinosoft.service.VoteService;
import cn.com.sinosoft.util.CronDateUtils;
@Component
public class IniterListener implements ApplicationListener<ContextRefreshedEvent>{
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private FxActivityService fxActivityService;
@Autowired
private VoteService voteService;
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {

//需要执行的监听逻辑写在该方法中,等到初始化完毕之后执行方法中的代码逻辑

	logger.info("bean容器初始化完成之后执行");
	try {
		//查询数据库中所有的活动,判断活动的有效状态,超期的置为无效,
		//Vote vote = new Vote();
		List<Vote> v = voteService.selectAllVoteForUsers();
		for(Vote votes : v) {
			
			Date date = new Date();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//当前系统时间
				Date d = sdf.parse(sdf.format(date));
				
			
				//Date open = sdf.parse(votes.getOpen_time());
				Date end = sdf.parse(votes.getEnd_time());
				//当前时间大于等于结束时间
				int i = d.compareTo(end);
				if(i==1||i==0) {
					
					//修改当前活动状态置为无效
					 int j = fxActivityService.updateActivityStates(votes.getId());
					 if(j==1) {
						 
						 logger.info("修改活动状态成功");
						 
					 }else {
						 
						 logger.info("修改活动状态失败,失败的活动名称为:"+votes.getId()+"名称:"+votes.getName());
						 
					 }
				}
				//截止时间大于当前时间,同时活动状态为有效
				if(i==-1&&votes.getStatus().equals("0")) {
					logger.info("添加活动到qz:活动名称为:"+votes.getName());
					//有效活动添加到qz中执行
					QuartzManager qm = new QuartzManager();
					qm.addJob(votes.getName(), "group"+votes.getName(), "trigger"+votes.getName(), "triggerGroup"+votes.getName(), 
							VoteJob.class,CronDateUtils.getCron(sdf.parse(votes.getEnd_time())),sdf.parse(votes.getOpen_time()), sdf.parse(votes.getEnd_time()),sdf.parse(votes.getOpen_time_join()),sdf.parse(votes.getEnd_time_join()),votes.getId());
				}else {
					logger.info("无效活动名称为:"+votes.getName());
				}
				
				
				
			
		}
		//未超期的活动注入到qz中动态加载
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}

如果报service链接数据库NullPointException异常的话可以尝试检查是否在application,或springmvc.xml中配置如下bean容器监听类

<context:component-scan base-package="cn.com.sinosoft.controller"></context:component-scan>
<!-- 初始化完成之后执行监听	 -->
<bean class="cn.com.sinosoft.lisener.IniterListener"/>

也可以尝试在web.xml中配置

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--注意自己的监听类一定要放在contextLoaderListener监听类之后	-->
<listener>
	<listener-class>
	cn.com.sinosoft.lisener.IniterListener
	</listener-class>
	
</listener>

如果监听类在初始化完成之后重复执行,可以尝试检查是否在多个位置同时该配置类监听,一般只配置一次的话不会执行多次该监听方式

以上就是自己的经历,大家可以参考一下,每个项目的业务场景不同但是差别不会太大

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值