Spring,SpringMvc初始化监听配置

 2023-09-11 阅读 14 评论 0

摘要:SpringMvc初始化完成之后启动监听配置方式主要是解决spring初始化过程中由于bean容器初始化顺序不同导致注入异常在springmvc.xml中通过bean注入监听器监听实现类实现ApplicationListenerpplicationListener接口触发ContextRefreshedEvent事件,重写onApplicationEvent

SpringMvc初始化完成之后启动监听配置方式

  • 主要是解决spring初始化过程中由于bean容器初始化顺序不同导致注入异常
    • 在springmvc.xml中通过bean注入监听器
    • 监听实现类实现ApplicationListenerpplicationListener接口触发ContextRefreshedEvent事件,重写onApplicationEvent方法
    • 如果报service链接数据库NullPointException异常的话可以尝试检查是否在application,或springmvc.xml中配置如下bean容器监听类
    • 也可以尝试在web.xml中配置
    • 如果监听类在初始化完成之后重复执行,可以尝试检查是否在多个位置同时该配置类监听,一般只配置一次的话不会执行多次该监听方式
      • 以上就是自己的经历,大家可以参考一下,每个项目的业务场景不同但是差别不会太大

主要是解决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 blocke.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>

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

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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/48222.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息