温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springmvc工程启动完成后执行初始化方法的示例分析

发布时间:2021-09-18 10:12:46 来源:亿速云 阅读:123 作者:柒染 栏目:编程语言

这篇文章给大家介绍springmvc工程启动完成后执行初始化方法的示例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

 在项目中,我们需要在应用启动完成后,执行特定的动作,比如调用第三方接口完成初始化。

实现

   1、实现接口ApplicationListener<ContextRefreshedEvent>,重写onApplicationEvent方法。

package com.iss.scheduler.task;

import com.iss.isp.commonadapter.service.camera.service.ICameraService;
import com.iss.models.commonadapter.constants.IpcCmdType;
import com.iss.models.commonadapter.pojo.DeviceCmd;
import com.j7cai.common.exception.FrameException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务启动时
 * 加载订阅元数据
 */
@Component
public class IpcActionLoader implements ApplicationListener<ContextRefreshedEvent> {

    private final static Logger logger = LoggerFactory.getLogger(IpcActionLoader.class);

    @Resource
    private ICameraService iCameraService;

//    @PostConstruct
//    public void init() {
//
//    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        // root application context 没有parent, 保证只有在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理
        if(contextRefreshedEvent.getApplicationContext().getParent() == null) {
            logger.info("begin to subscribe camera data.");

            // TODO 查询要订阅元数据的摄像机
            List<Long> deviceIdList = new ArrayList<>();
            deviceIdList.add(202103160927120057L);
            deviceIdList.add(202103160927120050L);
            DeviceCmd deviceCmd;
            for (Long deviceId:deviceIdList) {
                deviceCmd = new DeviceCmd();
                deviceCmd.setDeviceID(deviceId);
                try {
                    // 先停止取流 ,再调用IVS_PU_RealPlay接口取智能元数据流
                    deviceCmd.setCmdType(IpcCmdType.DIS_ARM);
                    iCameraService.deviceControlCmd(deviceCmd);
                    deviceCmd.setCmdType(IpcCmdType.ARM);
                    iCameraService.deviceControlCmd(deviceCmd);
                } catch (FrameException e) {
                    logger.error(deviceId + " subscribe camera data error." + e.toString(), e);
                }
            }
        }

    }
}

2、此时发现方法会被调用两次,原因如下:

applicationontext和使用MVC之后的webApplicationontext会两次调用上面的方法,如何区分这个两种容器呢? 

但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。 

这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码 

如下: 

    @Override  
      public void onApplicationEvent(ContextRefreshedEvent event) {  
        if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.  
             //需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。  
        }  
      }  

关于springmvc工程启动完成后执行初始化方法的示例分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI