In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you to springmvc project after the completion of the implementation of initialization method sample analysis, content is very detailed, interested in small partners can refer to reference, hope to be helpful to everyone.
In the project, we need to perform certain actions after the application is launched, such as calling a third-party interface to complete initialization.
achieve
Implement the interface ApplicationListener and override the onApplicationEvent method.
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;/** * Service startup * Load subscription metadata */@Componentpublic class IpcActionLoader implements ApplicationListener { 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 has no parent, ensuring that logic code is invoked only after initialization of root application context is completed, and no processing is done when initialization of other containers is completed. if(contextRefreshedEvent.getApplicationContext().getParent() == null) { logger.info("begin to subscribe camera data. "); // TODO queries cameras to subscribe metadata List deviceIdList = new ArrayList(); deviceIdList.add(202103160927120057L); deviceIdList.add(202103160927120050L); DeviceCmd deviceCmd; for (Long deviceId:deviceIdList) { deviceCmd = new DeviceCmd(); deviceCmd.setDeviceID(deviceId); try { //Stop fetching the stream first, and then call the IVS_PU_RealPlay interface to fetch the intelligent metadata stream 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. At this time, the discovery method will be called twice for the following reasons:
The application context and the webApplicationContext after using MVC will call the above method twice. How to distinguish between these two containers?
But at this point, there is a problem, in the web project (spring mvc), the system will exist two containers, one is the root application context , the other is our own projectName-servlet context (as a child of the root application context).
In this case, the onApplicationEvent method is executed twice. In order to avoid the problems mentioned above, we can only call the logic code after the initialization of the root application context is completed, and do nothing after the initialization of other containers.
As follows:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){//root application context has no parent, he is the boss.
//Logic code to execute, which will be executed when the Spring container is initialized.
}
}
On the springmvc project after the completion of the implementation of initialization method sample analysis will be shared here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.