diff --git a/src/main/java/cn/nkk/hikvision/config/HikVisionAutoConfiguration.java b/src/main/java/cn/nkk/hikvision/config/HikVisionAutoConfiguration.java index dcbf526..314bf63 100644 --- a/src/main/java/cn/nkk/hikvision/config/HikVisionAutoConfiguration.java +++ b/src/main/java/cn/nkk/hikvision/config/HikVisionAutoConfiguration.java @@ -32,45 +32,19 @@ @Import(SpringContextHolder.class) public class HikVisionAutoConfiguration { - private final static Logger log = LoggerFactory.getLogger(HikVisionAutoConfiguration.class); + private final static Logger log = LoggerFactory.getLogger(HikVisionAutoConfiguration.class); - private static final FIFOCache LOCAL_CACHE = CacheUtil.newFIFOCache(400); + private static final FIFOCache LOCAL_CACHE = CacheUtil.newFIFOCache(400); - private HCNetSDK hCNetSDK = null; + private HCNetSDK hCNetSDK = null; - private PlayCtrl playControl = null; + private final PlayCtrl playControl = null; - public HikVisionAutoConfiguration(HiKProperties properties){ - if(properties.getSdk_path()==null){ - throw new IllegalArgumentException("请指定sdk类库(注意区分系统版本)"); - } - - } - - @Bean - public HCNetSDK initSdk(HiKProperties properties){ - synchronized (HCNetSDK.class) { - String strDllPath = ""; - try { - //win系统加载库路径 - if (OsSelectUtil.isWindows()) { - strDllPath = properties.getSdk_path().startsWith("classpath") ? - ResourceUtils.getFile(properties.getSdk_path()+"/HCNetSDK.dll").getPath() : properties.getSdk_path()+"/HCNetSDK.dll"; - } - //Linux系统加载库路径 - else if (OsSelectUtil.isLinux()){ - strDllPath = properties.getSdk_path().startsWith("classpath") ? - ResourceUtils.getFile(properties.getSdk_path()+"/libhcnetsdk.so").getPath() : properties.getSdk_path()+"/libhcnetsdk.so"; - } - hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class); - } catch (Exception ex) { - log.error("加载HikSdk失败"); - ex.printStackTrace(); - return null; - } + public HikVisionAutoConfiguration(HiKProperties properties) { + if (properties.getSdk_path() == null) { + throw new IllegalArgumentException("请指定sdk类库(注意区分系统版本)"); } - log.info("加载hksdk成功"); - return hCNetSDK; + } @Bean @@ -83,12 +57,12 @@ private static PlayCtrl initPlay(HiKProperties properties) { //win系统加载库路径 if (OsSelectUtil.isWindows()) strPlayPath = properties.getSdk_path().startsWith("classpath") ? - ResourceUtils.getFile(properties.getSdk_path()+"/PlayCtrl.dll").getPath() : properties.getSdk_path()+"/PlayCtrl.dll"; - //Linux系统加载库路径 + ResourceUtils.getFile(properties.getSdk_path() + "/PlayCtrl.dll").getPath() : properties.getSdk_path() + "/PlayCtrl.dll"; + //Linux系统加载库路径 else if (OsSelectUtil.isLinux()) strPlayPath = properties.getSdk_path().startsWith("classpath") ? - ResourceUtils.getFile(properties.getSdk_path()+"/libPlayCtrl.so").getPath() : properties.getSdk_path()+"/libPlayCtrl.so"; - playControl = (PlayCtrl) Native.loadLibrary(strPlayPath,PlayCtrl.class); + ResourceUtils.getFile(properties.getSdk_path() + "/libPlayCtrl.so").getPath() : properties.getSdk_path() + "/libPlayCtrl.so"; + playControl = (PlayCtrl) Native.loadLibrary(strPlayPath, PlayCtrl.class); } catch (Exception ex) { log.error("加载playControl失败}"); ex.printStackTrace(); @@ -99,6 +73,32 @@ else if (OsSelectUtil.isLinux()) return playControl; } + @Bean + public HCNetSDK initSdk(HiKProperties properties) { + synchronized (HCNetSDK.class) { + String strDllPath = ""; + try { + //win系统加载库路径 + if (OsSelectUtil.isWindows()) { + strDllPath = properties.getSdk_path().startsWith("classpath") ? + ResourceUtils.getFile(properties.getSdk_path() + "/HCNetSDK.dll").getPath() : properties.getSdk_path() + "/HCNetSDK.dll"; + } + //Linux系统加载库路径 + else if (OsSelectUtil.isLinux()) { + strDllPath = properties.getSdk_path().startsWith("classpath") ? + ResourceUtils.getFile(properties.getSdk_path() + "/libhcnetsdk.so").getPath() : properties.getSdk_path() + "/libhcnetsdk.so"; + } + hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class); + } catch (Exception ex) { + log.error("加载HikSdk失败"); + ex.printStackTrace(); + return null; + } + } + log.info("加载hksdk成功"); + return hCNetSDK; + } + @Bean("converterPoolExecutor") public ThreadPoolTaskExecutor asyncServiceExecutor(HiKProperties hiKProperties) { @@ -123,7 +123,7 @@ public ThreadPoolTaskExecutor asyncServiceExecutor(HiKProperties hiKProperties) } @PreDestroy - public void destroy(){ + public void destroy() { HkUtils.stopListen(); if (null != hCNetSDK) { log.info("释放hcNetSDK资源"); diff --git a/src/main/java/cn/nkk/hikvision/events/FExceptionCallBack.java b/src/main/java/cn/nkk/hikvision/events/FExceptionCallBack.java index 51b1be9..b7c58b4 100644 --- a/src/main/java/cn/nkk/hikvision/events/FExceptionCallBack.java +++ b/src/main/java/cn/nkk/hikvision/events/FExceptionCallBack.java @@ -15,14 +15,13 @@ public class FExceptionCallBack implements HCNetSDK.FExceptionCallBack { @Override public void invoke(int dwType, int lUserID, int lHandle, Pointer pUser) { - log.info("设备异常,dwType:{},userId:{},handler:{}",dwType,lUserID,lHandle); - switch(dwType) - { + log.info("设备异常,dwType:{},userId:{},handler:{}", dwType, lUserID, lHandle); + switch (dwType) { case EXCEPTION_EXCHANGE: //用户交互时异常 - log.error("用户交互时异常:{}",lHandle); + log.error("用户交互时异常:{}", lHandle); break; case 0x8017: - log.error("用户交互恢复:{}",lHandle); + log.error("用户交互恢复:{}", lHandle); break; default: System.out.println("异常"); diff --git a/src/main/java/cn/nkk/hikvision/factory/FlvConverter.java b/src/main/java/cn/nkk/hikvision/factory/FlvConverter.java index 7c85c48..365b0a5 100644 --- a/src/main/java/cn/nkk/hikvision/factory/FlvConverter.java +++ b/src/main/java/cn/nkk/hikvision/factory/FlvConverter.java @@ -25,13 +25,14 @@ * @author dlj * @date 2023/03/29 */ + /** * flv转换器 * * @author dlj * @date 2023/03/29 */ -public class FlvConverter extends Thread implements Converter{ +public class FlvConverter extends Thread implements Converter { private static final Logger log = LoggerFactory.getLogger(FlvConverter.class); private byte[] headers; @@ -69,14 +70,14 @@ public void run() { try { avutil.av_log_set_level(avutil.AV_LOG_ERROR); log.info("进入grabber------------------------"); - grabber = Objects.nonNull(inputStream) ? new FFmpegFrameGrabber(inputStream,0) : new FFmpegFrameGrabber(rtspUrl); + grabber = Objects.nonNull(inputStream) ? new FFmpegFrameGrabber(inputStream, 0) : new FFmpegFrameGrabber(rtspUrl); if (StrUtil.isNotEmpty(rtspUrl) && rtspUrl.startsWith("rtsp")) { grabber.setOption("rtsp_transport", "tcp"); //首选TCP进行RTP传输 grabber.setOption("rtsp_flags", "prefer_tcp"); log.info("rtsp链接------------------------"); } - if(Objects.nonNull(inputStream)){ + if (Objects.nonNull(inputStream)) { //检测管道流中是否存在数据,如果2s后依然没有写入1024的数据,则认为管道流中无数据,避免grabber.start();发生阻塞 long stime = new Date().getTime(); while (true) { @@ -93,7 +94,7 @@ public void run() { grabber.setOption("buffer_size", "1024000"); grabber.startUnsafe(); int videoCodec = grabber.getVideoCodec(); - log.info("启动grabber,编码{}",videoCodec); + log.info("启动grabber,编码{}", videoCodec); grabber.setImageWidth(640); grabber.setImageHeight(480); @@ -141,20 +142,20 @@ public void run() { TimeUnit.MILLISECONDS.sleep(5); } } catch (Exception e) { - log.error("异步出错,{}",e.getMessage()); + log.error("异步出错,{}", e.getMessage()); } finally { try { - if(this.outputStream!=null) this.outputStream.close(); - if(this.inputStream != null) this.inputStream.close(); - if(this.playHandler!= null) HkUtils.stopBackPlay(this.playHandler); - if(grabber!= null) grabber.close(); + if (this.outputStream != null) this.outputStream.close(); + if (this.inputStream != null) this.inputStream.close(); + if (this.playHandler != null) HkUtils.stopBackPlay(this.playHandler); + if (grabber != null) grabber.close(); if (recorder != null) recorder.close(); if (stream != null) stream.close(); System.gc(); context.getResponse().flushBuffer(); context.complete(); } catch (Exception e) { - log.error("资源回收,{}",e.getMessage()); + log.error("资源回收,{}", e.getMessage()); } } @@ -175,7 +176,6 @@ public void writeResponse(byte[] bytes) { } - @Override public String getKey() { return null; diff --git a/src/main/java/cn/nkk/hikvision/factory/M3u8Converter.java b/src/main/java/cn/nkk/hikvision/factory/M3u8Converter.java index fbf7ad0..7c01f90 100644 --- a/src/main/java/cn/nkk/hikvision/factory/M3u8Converter.java +++ b/src/main/java/cn/nkk/hikvision/factory/M3u8Converter.java @@ -25,13 +25,13 @@ * @author dlj * @date 2023/03/29 */ -public class M3u8Converter extends Thread implements Converter{ +public class M3u8Converter extends Thread implements Converter { private final static Logger log = LoggerFactory.getLogger(M3u8Converter.class); private final String rtspUrl; - public M3u8Converter(String rtspUrl){ + public M3u8Converter(String rtspUrl) { this.rtspUrl = rtspUrl; } @@ -39,13 +39,13 @@ public M3u8Converter(String rtspUrl){ @Override public void run() { HiKProperties hiKProperties = SpringContextHolder.getBean(HiKProperties.class); - if(StrUtil.isEmpty(hiKProperties.getStream().getM3u8_path())){ + if (StrUtil.isEmpty(hiKProperties.getStream().getM3u8_path())) { throw new IllegalArgumentException("请配置m3u8视频流存储路径"); } String md5 = Md5Utils.encrypt16(rtspUrl); File file = new File(hiKProperties.getStream().getM3u8_path(), md5); - if(!file.exists()){ + if (!file.exists()) { FileUtil.mkdir(file); } // 存放m3u8切片视频的目录 @@ -120,7 +120,7 @@ public void run() { continue; } if (pkt.dts() == avutil.AV_NOPTS_VALUE && pkt.pts() == avutil.AV_NOPTS_VALUE || pkt.pts() < dts) { - log.debug("异常pkt 当前pts: " + pkt.pts() + " dts: " + pkt.dts() + " 上一包的pts: " + pts + " dts: "+ dts); + log.debug("异常pkt 当前pts: " + pkt.pts() + " dts: " + pkt.dts() + " 上一包的pts: " + pts + " dts: " + dts); av_packet_unref(pkt); continue; } @@ -133,8 +133,7 @@ public void run() { //取出过滤器合并后的图像 // Frame filterFrame=filter.pullImage(); long spull_end = System.currentTimeMillis(); - if ((spull_end - spull_start) > 500) - log.info("pull stream take up time : {}", spull_end - spull_start); + if ((spull_end - spull_start) > 500) log.info("pull stream take up time : {}", spull_end - spull_start); recorder.recordPacket(pkt); timebase = grabber.getFormatContext().streams(pkt.stream_index()).time_base().den(); pts += (timebase / (int) framerate); @@ -147,12 +146,12 @@ public void run() { //可以在这里写个错误图片流返回,这样播放器就能看到问题了 } finally { try { - if(Objects.nonNull(grabber)) grabber.close(); - if(Objects.nonNull(recorder)) recorder.close(); + if (Objects.nonNull(grabber)) grabber.close(); + if (Objects.nonNull(recorder)) recorder.close(); FileUtil.del(m3u8Path); System.gc(); } catch (Exception e) { - log.error("资源回收:{}",e.getMessage()); + log.error("资源回收:{}", e.getMessage()); } } } diff --git a/src/main/java/cn/nkk/hikvision/properties/HiKProperties.java b/src/main/java/cn/nkk/hikvision/properties/HiKProperties.java index 9048651..9c4bc31 100644 --- a/src/main/java/cn/nkk/hikvision/properties/HiKProperties.java +++ b/src/main/java/cn/nkk/hikvision/properties/HiKProperties.java @@ -8,24 +8,53 @@ @ConfigurationProperties(prefix = "hik") public class HiKProperties { - /** - * 海康sdk位置 - */ - private String sdk_path; - - /** * 拉流配置 */ public PullStream stream; - + /** + * 海康sdk位置 + */ + private String sdk_path; /** * 线程池配置 */ private HikPool pool; + public String getSdk_path() { + return sdk_path; + } + + public void setSdk_path(String sdk_path) { + this.sdk_path = sdk_path; + } + + public PullStream getStream() { + return stream; + } + + public void setStream(PullStream stream) { + this.stream = stream; + } + + public HikPool getPool() { + return pool; + } + + public void setPool(HikPool pool) { + this.pool = pool; + } + + @Override + public String toString() { + return "HiKProperties{" + + "sdk_path='" + sdk_path + '\'' + + ", stream=" + stream + + ", pool=" + pool + + '}'; + } - public static class HikPool{ + public static class HikPool { /** * 核心线程数 */ @@ -90,8 +119,7 @@ public String toString() { } } - - public static class PullStream{ + public static class PullStream { /** * m3u8磁盘路径 @@ -113,38 +141,4 @@ public String toString() { '}'; } } - - - public String getSdk_path() { - return sdk_path; - } - - public void setSdk_path(String sdk_path) { - this.sdk_path = sdk_path; - } - - public PullStream getStream() { - return stream; - } - - public void setStream(PullStream stream) { - this.stream = stream; - } - - public HikPool getPool() { - return pool; - } - - public void setPool(HikPool pool) { - this.pool = pool; - } - - @Override - public String toString() { - return "HiKProperties{" + - "sdk_path='" + sdk_path + '\'' + - ", stream=" + stream + - ", pool=" + pool + - '}'; - } } diff --git a/src/main/java/cn/nkk/hikvision/utils/HkUtils.java b/src/main/java/cn/nkk/hikvision/utils/HkUtils.java index d516f16..58ca5bf 100644 --- a/src/main/java/cn/nkk/hikvision/utils/HkUtils.java +++ b/src/main/java/cn/nkk/hikvision/utils/HkUtils.java @@ -33,8 +33,8 @@ /** *

海康威视工具包

- * @author dlj * + * @author dlj */ public final class HkUtils { @@ -56,7 +56,7 @@ public final class HkUtils { //这里是库的绝对路径,请根据实际情况修改,注意改路径必须有访问权限 String strPath1 = strPathCom + "/libcrypto.so"; - String strPath2 = strPathCom + "/libssl.so"; + String strPath2 = strPathCom + "/libssl.so"; System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length()); ptrByteArray1.write(); @@ -80,18 +80,18 @@ public final class HkUtils { * @param serialNumber 设备序列号 * @return {@link Boolean} */ - public static Boolean hasOnline(String serialNumber){ + public static Boolean hasOnline(String serialNumber) { try { CameraLogin cameraLogin = LOCAL_CACHE.get("serialNumber"); HCNetSDK.NET_DVR_WORKSTATE_V30 workState = new HCNetSDK.NET_DVR_WORKSTATE_V30(); boolean result = hcNetSDK.NET_DVR_GetDVRWorkState_V30(cameraLogin.getUserId(), workState); if (!result) { - log.error("获取设备{}状态失败",serialNumber); + log.error("获取设备{}状态失败", serialNumber); return false; } - log.info("设备状态:{}",workState.dwDeviceStatic); - }catch (Exception e){ - log.error("获取设备{}状态失败",e.getMessage()); + log.info("设备状态:{}", workState.dwDeviceStatic); + } catch (Exception e) { + log.error("获取设备{}状态失败", e.getMessage()); } return false; } @@ -103,23 +103,23 @@ public static Boolean hasOnline(String serialNumber){ * @param serialNumber 序列号 * @return {@link CameraLogin} */ - public static CameraLogin loginProfile(String serialNumber){ + public static CameraLogin loginProfile(String serialNumber) { return LOCAL_CACHE.get(serialNumber); } /** - * * 设备登陆 + * * @param ip ip * @param port 端口 * @param userName 用户名 * @param password 密码 * @return {@link CameraLogin} */ - public static CameraLogin doLogin(String ip, String port, String userName, String password){ + public static CameraLogin doLogin(String ip, String port, String userName, String password) { hcNetSDK.NET_DVR_Init(); - hcNetSDK.NET_DVR_SetConnectTime(2000,1); - hcNetSDK.NET_DVR_SetReconnect(10000,true); + hcNetSDK.NET_DVR_SetConnectTime(2000, 1); + hcNetSDK.NET_DVR_SetReconnect(10000, true); HCNetSDK.NET_DVR_LOCAL_GENERAL_CFG struNET_DVR_LOCAL_GENERAL_CFG = new HCNetSDK.NET_DVR_LOCAL_GENERAL_CFG(); struNET_DVR_LOCAL_GENERAL_CFG.byAlarmJsonPictureSeparate = 1; //设置JSON透传报警数据和图片分离 @@ -131,8 +131,8 @@ public static CameraLogin doLogin(String ip, String port, String userName, Strin int userId = hcNetSDK.NET_DVR_Login_V30(ip, Short.parseShort(port), userName, password, m_strDeviceInfo); //如果注册失败返回-1,获取错误码 - if ( userId < 0 ){ - log.error("设备{}:{},注册失败:{}",ip,port,hcNetSDK.NET_DVR_GetLastError()); + if (userId < 0) { + log.error("设备{}:{},注册失败:{}", ip, port, hcNetSDK.NET_DVR_GetLastError()); throw new RuntimeException("登陆失败"); } @@ -150,10 +150,10 @@ public static CameraLogin doLogin(String ip, String port, String userName, Strin // 监听异常信息 HCNetSDK.FExceptionCallBack fExceptionCallBack = new FExceptionCallBack(); - hcNetSDK.NET_DVR_SetExceptionCallBack_V30(0,0,fExceptionCallBack, pUser); + hcNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, pUser); - log.info("设备{}:{},注册成功,userId={},设备编号:{},设备数组通道总数:{},",ip,port,userId,serialNumber,maxIpChannelNum); - LOCAL_CACHE.put(serialNumber,cameraInfo); + log.info("设备{}:{},注册成功,userId={},设备编号:{},设备数组通道总数:{},", ip, port, userId, serialNumber, maxIpChannelNum); + LOCAL_CACHE.put(serialNumber, cameraInfo); return cameraInfo; } @@ -163,26 +163,24 @@ public static CameraLogin doLogin(String ip, String port, String userName, Strin * @param deviceInfo 设备信息 * @return int */ - private static int getChannelNum(HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo){ - int maxIpChannelNum ; - if (deviceInfo.byHighDChanNum == 0) - { + private static int getChannelNum(HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo) { + int maxIpChannelNum; + if (deviceInfo.byHighDChanNum == 0) { maxIpChannelNum = deviceInfo.byIPChanNum & 0xff; - } - else - { - maxIpChannelNum = (int)((deviceInfo.byHighDChanNum & 0xff) << 8); + } else { + maxIpChannelNum = (int) ((deviceInfo.byHighDChanNum & 0xff) << 8); } return maxIpChannelNum; } /** * 获取Ip通道 - * @param lUserID 用户id

登陆成功后返回 + * + * @param lUserID 用户id

登陆成功后返回 * @param deviceInfo 设备信息 * @return */ - private static List getChannelNumber(int lUserID, int maxIpChannelNum, HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo){ + private static List getChannelNumber(int lUserID, int maxIpChannelNum, HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo) { List cameraChannels = new ArrayList<>(); //DVR工作状态 HCNetSDK.NET_DVR_WORKSTATE_V30 devWork = new HCNetSDK.NET_DVR_WORKSTATE_V30(); @@ -202,7 +200,7 @@ private static List getChannelNumber(int lUserID, int if (!bRet) { //设备不支持,则表示没有IP通道 for (int chanNum = 0; chanNum < deviceInfo.byChanNum; chanNum++) { - log.debug("Camera{}",(chanNum + deviceInfo.byStartChan)); + log.debug("Camera{}", (chanNum + deviceInfo.byStartChan)); } return null; } @@ -214,8 +212,8 @@ private static List getChannelNumber(int lUserID, int dvrStreamMode.uGetStream.setType(HCNetSDK.NET_DVR_IPCHANINFO.class); dvrStreamMode.uGetStream.struChanInfo.read(); HCNetSDK.NET_DVR_IPDEVINFO_V31 dvrIpInfo = m_strIpparaCfg.struIPDevInfo[chanNum]; - int channelID = hcNetSDK.NET_DVR_SDKChannelToISAPI(lUserID,chanNum + deviceInfo.byStartDChan,true); - int devworkChannels =(chanNum + deviceInfo.byStartDChan-1); + int channelID = hcNetSDK.NET_DVR_SDKChannelToISAPI(lUserID, chanNum + deviceInfo.byStartDChan, true); + int devworkChannels = (chanNum + deviceInfo.byStartDChan - 1); // 设置参数 CameraLogin.CameraChannel channel = new CameraLogin.CameraChannel(); @@ -245,7 +243,7 @@ private static List getChannelNumber(int lUserID, int public static int setupAlarmChan(int userId, HCNetSDK.FMSGCallBack_V31 callBack) { boolean b = hcNetSDK.NET_DVR_SetDVRMessageCallBack_V31(callBack, null); - if(b){ + if (b) { log.info("设置回调成功"); } @@ -256,15 +254,15 @@ public static int setupAlarmChan(int userId, HCNetSDK.FMSGCallBack_V31 callBack) m_strAlarmInfo.byAlarmInfoType = 1; //上传报警信息类型: 0- 老报警信息(NET_DVR_PLATE_RESULT), 1- 新报警信息(NET_ITS_PLATE_RESULT) m_strAlarmInfo.byDeployType = 1; //布防类型:0-客户端布防,1-实时布防 m_strAlarmInfo.write(); - int lAlarmHandle = hcNetSDK.NET_DVR_SetupAlarmChan_V41(userId, m_strAlarmInfo); + int lAlarmHandle = hcNetSDK.NET_DVR_SetupAlarmChan_V41(userId, m_strAlarmInfo); //如果布防失败返回-1 - if (lAlarmHandle < 0){ - log.error("布防失败! code:{}",hcNetSDK.NET_DVR_GetLastError()); + if (lAlarmHandle < 0) { + log.error("布防失败! code:{}", hcNetSDK.NET_DVR_GetLastError()); hcNetSDK.NET_DVR_Logout(userId); //注销 hcNetSDK.NET_DVR_Cleanup(); //释放SDK资源 } - log.info("布防成功:{}",lAlarmHandle ); - return lAlarmHandle ; + log.info("布防成功:{}", lAlarmHandle); + return lAlarmHandle; } /** @@ -273,7 +271,7 @@ public static int setupAlarmChan(int userId, HCNetSDK.FMSGCallBack_V31 callBack) * @param alarmHandle 布防成功返回的id */ public static void closeAlarmChan(Integer alarmHandle) { - if (alarmHandle> -1) { + if (alarmHandle > -1) { if (!hcNetSDK.NET_DVR_CloseAlarmChan_V30(alarmHandle)) { log.error("撤防失败"); } @@ -287,10 +285,10 @@ public static void closeAlarmChan(Integer alarmHandle) { * * @param userId 用户id */ - public static void doLogout(int userId){ + public static void doLogout(int userId) { hcNetSDK.NET_DVR_Logout(userId); hcNetSDK.NET_DVR_Cleanup(); - log.info("设备:{}注销完成",userId); + log.info("设备:{}注销完成", userId); } /** @@ -298,14 +296,14 @@ public static void doLogout(int userId){ * * @param serialNumber 序列号 */ - public static void doLogout(String serialNumber){ + public static void doLogout(String serialNumber) { CameraLogin cameraLogin = LOCAL_CACHE.get(serialNumber); - if(Objects.isNull(cameraLogin)){ + if (Objects.isNull(cameraLogin)) { return; } hcNetSDK.NET_DVR_Logout(cameraLogin.getUserId()); hcNetSDK.NET_DVR_Cleanup(); - log.info("设备:{}注销完成",cameraLogin.getUserId()); + log.info("设备:{}注销完成", cameraLogin.getUserId()); LOCAL_CACHE.remove(serialNumber); } @@ -313,110 +311,112 @@ public static void doLogout(String serialNumber){ /** * 侦听端口 * - * @param ip ip - * @param port 端口 + * @param ip ip + * @param port 端口 * @param callback 监听回调 */ - public static void startListen(String ip, String port, HCNetSDK.FMSGCallBack_V31 callback){ + public static void startListen(String ip, String port, HCNetSDK.FMSGCallBack_V31 callback) { int lListenHandle = hcNetSDK.NET_DVR_StartListen_V30(ip, Short.parseShort(port), callback, null); if (lListenHandle == -1) { log.error("监听失败" + hcNetSDK.NET_DVR_GetLastError()); throw new RuntimeException("监听失败"); } - log.info("监听成功!ip:{},port:{}",ip , port); + log.info("监听成功!ip:{},port:{}", ip, port); } /** * 停止端口监听 */ - public static void stopListen(){ + public static void stopListen() { hcNetSDK.NET_DVR_StopListen(); } /** - * *

下载回放视频

- * @param userId 登陆返回的id + * + * @param userId 登陆返回的id * @param channelNum 通道编号 - * @param beginTime 开始时间 - * @param endTime 结束时间 - * @param disk 磁盘目录 + * @param beginTime 开始时间 + * @param endTime 结束时间 + * @param disk 磁盘目录 */ - public static String downloadFileToDisk(int userId, int channelNum, String beginTime, String endTime, File disk) throws Exception{ + public static String downloadFileToDisk(int userId, int channelNum, String beginTime, String endTime, File disk) throws Exception { - if(FileUtil.isFile(disk)){ + if (FileUtil.isFile(disk)) { throw new RuntimeException("disk参数必须是磁盘目录"); } - if(!FileUtil.exist(disk)){ + if (!FileUtil.exist(disk)) { FileUtil.mkdir(disk); } // 构建下载参数 HCNetSDK.NET_DVR_PLAYCOND downloadData = new HCNetSDK.NET_DVR_PLAYCOND(); downloadData.dwChannel = channelNum; - downloadData.struStartTime = CommonUtil.getNvrTime(DateUtil.parseDateTime(beginTime)); + downloadData.struStartTime = CommonUtil.getNvrTime(DateUtil.parseDateTime(beginTime)); downloadData.struStopTime = CommonUtil.getNvrTime(DateUtil.parseDateTime(endTime)); downloadData.byDrawFrame = 0; downloadData.byStreamType = 0; - String videoPath = disk.getPath()+ File.separator + UUID.randomUUID().toString()+".mp4"; + String videoPath = disk.getPath() + File.separator + UUID.randomUUID().toString() + ".mp4"; int downloadRes = hcNetSDK.NET_DVR_GetFileByTime_V40(userId, videoPath, downloadData); - if(downloadRes<0){ + if (downloadRes < 0) { int errorCode = hcNetSDK.NET_DVR_GetLastError(); - throw new RuntimeException("下载失败,code:"+errorCode); + throw new RuntimeException("下载失败,code:" + errorCode); } - log.info("下载任务句柄:{}",downloadRes); + log.info("下载任务句柄:{}", downloadRes); hcNetSDK.NET_DVR_PlayBackControl(downloadRes, HCNetSDK.NET_DVR_PLAYSTART, 0, null); log.info("开始下载...."); - while (true){ + while (true) { IntByReference LPOutValue = new IntByReference(); hcNetSDK.NET_DVR_PlayBackControl(downloadRes, HCNetSDK.NET_DVR_PLAYGETPOS, 0, LPOutValue); int progress = LPOutValue.getValue(); - if(progress==100) { - log.info("文件:{},下载完成",videoPath); + if (progress == 100) { + log.info("文件:{},下载完成", videoPath); break; } } // 释放查询资源 hcNetSDK.NET_DVR_StopGetFile(downloadRes); - + // 转换成mp4格式,否者在linux下无法播放 - String convertPath = disk.getPath()+ File.separator + UUID.randomUUID().toString()+".mp4"; - try{ + String convertPath = disk.getPath() + File.separator + UUID.randomUUID().toString() + ".mp4"; + try { boolean complete = VideoConvertUtil.convert(videoPath, convertPath); - if(complete){ + if (complete) { FileUtil.del(videoPath); return convertPath; } - }catch (Exception ignore) {} - + } catch (Exception ignore) { + } + return videoPath; } /** *

查询回放视频 - * @param userId 登陆返回的id + * + * @param userId 登陆返回的id * @param channelNum 通道编号 - * @param beginTime 开始时间 - * @param endTime 结束时间 + * @param beginTime 开始时间 + * @param endTime 结束时间 */ - public static List findFile(int userId , int channelNum, String beginTime, String endTime){ + public static List findFile(int userId, int channelNum, String beginTime, String endTime) { /** * 构建查询条件 */ HCNetSDK.NET_DVR_FILECOND findData = new HCNetSDK.NET_DVR_FILECOND(); - findData.dwFileType=0; + findData.dwFileType = 0; findData.lChannel = channelNum; findData.struStartTime = CommonUtil.getNvrTime(DateUtil.parseDateTime(beginTime)); findData.struStopTime = CommonUtil.getNvrTime(DateUtil.parseDateTime(endTime)); - int v30FindRes = hcNetSDK.NET_DVR_FindFile_V30(userId,findData); - if(v30FindRes<0){ + int v30FindRes = hcNetSDK.NET_DVR_FindFile_V30(userId, findData); + if (v30FindRes < 0) { int v30ErrorCode = hcNetSDK.NET_DVR_GetLastError(); - throw new RuntimeException("文件查询失败,code:"+v30ErrorCode); + throw new RuntimeException("文件查询失败,code:" + v30ErrorCode); } - log.info("文件查询成功,句柄:{}",v30FindRes); + log.info("文件查询成功,句柄:{}", v30FindRes); HCNetSDK.NET_DVR_FINDDATA_V30 nextFind = new HCNetSDK.NET_DVR_FINDDATA_V30(); int nextFile = 0; @@ -425,9 +425,9 @@ public static List findFile(int userId , int channelNum, String begin // 逐个获取查找到的文件信息 nextFile = hcNetSDK.NET_DVR_FindNextFile_V30(v30FindRes, nextFind); // 找到文件 - if(nextFile == HCNetSDK.NET_DVR_FILE_SUCCESS){ + if (nextFile == HCNetSDK.NET_DVR_FILE_SUCCESS) { String fileName = new String(nextFind.sFileName).split("\0", 2)[0]; - double fileSize = NumberUtil.div(nextFind.dwFileSize * 1.0,1024 * 1024,2); + double fileSize = NumberUtil.div(nextFind.dwFileSize * 1.0, 1024 * 1024, 2); VideoFile videoFile = new VideoFile(); videoFile.setFileName(fileName); @@ -435,7 +435,7 @@ public static List findFile(int userId , int channelNum, String begin videoFile.setBeginTime(nextFind.struStartTime.toStringTime()); videoFile.setEndTime(nextFind.struStopTime.toStringTime()); fileList.add(videoFile); - }else if (nextFile == HCNetSDK.NET_DVR_NOMOREFILE) { + } else if (nextFile == HCNetSDK.NET_DVR_NOMOREFILE) { log.info("查找结束, 没有更多的文件"); break; } @@ -449,32 +449,32 @@ public static List findFile(int userId , int channelNum, String begin /** * 实时预览视频 - * @param userId 登陆返回的id - * @param channelNum 通道编号 * + * @param userId 登陆返回的id + * @param channelNum 通道编号 * @return { @link VideoPreview } 返回预览数据 */ - public static VideoPreview startRelaPlay(int userId, int channelNum){ + public static VideoPreview startRelaPlay(int userId, int channelNum) { HCNetSDK.NET_DVR_PREVIEWINFO strClientInfo = new HCNetSDK.NET_DVR_PREVIEWINFO(); strClientInfo.read(); strClientInfo.hPlayWnd = null; //窗口句柄,从回调取流不显示一般设置为空 strClientInfo.lChannel = channelNum; //通道号 - strClientInfo.dwStreamType=0; //0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推 - strClientInfo.dwLinkMode=0; //连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4- RTP/RTSP,5- RTP/HTTP,6- HRUDP(可靠传输) ,7- RTSP/HTTPS,8- NPQ - strClientInfo.bBlocked=1; + strClientInfo.dwStreamType = 0; //0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推 + strClientInfo.dwLinkMode = 0; //连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4- RTP/RTSP,5- RTP/HTTP,6- HRUDP(可靠传输) ,7- RTSP/HTTPS,8- NPQ + strClientInfo.bBlocked = 1; strClientInfo.write(); try { PipedOutputStream outputStream = new PipedOutputStream(); RealDataCallBack realDataCallBack = SpringContextHolder.getBean(RealDataCallBack.class); - int playHandle = hcNetSDK.NET_DVR_RealPlay_V40(userId, strClientInfo, realDataCallBack , null); - if(playHandle==-1){ + int playHandle = hcNetSDK.NET_DVR_RealPlay_V40(userId, strClientInfo, realDataCallBack, null); + if (playHandle == -1) { int iErr = hcNetSDK.NET_DVR_GetLastError(); - log.error("取流失败,错误码:{}" ,iErr); + log.error("取流失败,错误码:{}", iErr); throw new RuntimeException("取流失败"); } - realDataCallBack.outputStreamMap.put(String.valueOf(playHandle),outputStream); + realDataCallBack.outputStreamMap.put(String.valueOf(playHandle), outputStream); log.info("取流成功"); VideoPreview videoPreview = new VideoPreview(); videoPreview.setPlayHandler(playHandle); @@ -486,19 +486,19 @@ public static VideoPreview startRelaPlay(int userId, int channelNum){ videoPreview.setEndTime(""); return videoPreview; - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); return null; } } - /** * 停止实时播放 + * * @param playHandle 播放id */ - public static void stopRelaPlay(int playHandle){ + public static void stopRelaPlay(int playHandle) { log.info("停止实时预览"); hcNetSDK.NET_DVR_StopRealPlay(playHandle); @@ -506,14 +506,14 @@ public static void stopRelaPlay(int playHandle){ /** * 播放回放视频 - * @param userId 登陆返回的id - * @param channelNum 通道编号 - * @param beginTime 开始时间 - * @param endTime 结束时间 * - * @return { @link VideoPreview } 返回预览数据 + * @param userId 登陆返回的id + * @param channelNum 通道编号 + * @param beginTime 开始时间 + * @param endTime 结束时间 + * @return { @link VideoPreview } 返回预览数据 */ - public static VideoPreview startBackPlay(int userId, int channelNum, String beginTime, String endTime){ + public static VideoPreview startBackPlay(int userId, int channelNum, String beginTime, String endTime) { int playHandle = hcNetSDK.NET_DVR_PlayBackByTime( userId, @@ -522,17 +522,17 @@ public static VideoPreview startBackPlay(int userId, int channelNum, String begi CommonUtil.getNvrTime(DateUtil.parseDateTime(endTime)) , null); - if(playHandle<0){ + if (playHandle < 0) { int v30ErrorCode = hcNetSDK.NET_DVR_GetLastError(); - throw new RuntimeException("文件查询失败,code:"+v30ErrorCode); + throw new RuntimeException("文件查询失败,code:" + v30ErrorCode); } PipedOutputStream outputStream = new PipedOutputStream(); BackDataCallBack backDataCallBack = SpringContextHolder.getBean(BackDataCallBack.class); - backDataCallBack.outputStreamMap.put(StrUtil.format("{}-{}",userId,playHandle),outputStream); + backDataCallBack.outputStreamMap.put(StrUtil.format("{}-{}", userId, playHandle), outputStream); // 注册回调函数 - hcNetSDK.NET_DVR_SetPlayDataCallBack(playHandle, backDataCallBack,userId); + hcNetSDK.NET_DVR_SetPlayDataCallBack(playHandle, backDataCallBack, userId); // 控制录像回放状态 开始回放 hcNetSDK.NET_DVR_PlayBackControl(playHandle, HCNetSDK.NET_DVR_PLAYSTART, 0, null); // 控制历史回放拉流推流时的速度和直播一致 @@ -551,12 +551,13 @@ public static VideoPreview startBackPlay(int userId, int channelNum, String begi /** * 停止回放播放 + * * @param playHandle 播放id */ - public static void stopBackPlay(int playHandle){ + public static void stopBackPlay(int playHandle) { hcNetSDK.NET_DVR_PlayBackControl(playHandle, HCNetSDK.NET_DVR_PLAYSTOPAUDIO, 0, null); hcNetSDK.NET_DVR_StopPlayBack(playHandle); - log.info("停止回放预览{}",playHandle); + log.info("停止回放预览{}", playHandle); } @@ -566,13 +567,13 @@ public static void stopBackPlay(int playHandle){ * @param playHandler 播放句柄 * @return File 返回文件 */ - public static File captureImage(int playHandler){ + public static File captureImage(int playHandler) { // 获取jar包所在的位置,保存抓图结果 ApplicationHome home = new ApplicationHome(HkUtils.class); - File source = new File(home.getSource(),"capture"); + File source = new File(home.getSource(), "capture"); boolean isSuccess = hcNetSDK.NET_DVR_PlayBackCaptureFile(playHandler, source.getParentFile().toString()); - if(isSuccess){ + if (isSuccess) { return source; } throw new RuntimeException("抓图失败"); @@ -580,7 +581,6 @@ public static File captureImage(int playHandler){ /** - * *

rtsp 实时流推送地址

* * @param ip ip @@ -590,15 +590,12 @@ public static File captureImage(int playHandler){ * @param channelNum 通道编号 * @return {@link String} 返回推流结果 */ - public static String toRtspUrl(String ip, String port, String userName, String password, int channelNum){ - return StrUtil.format("rtsp://{}:{}@{}:{}/Streaming/Channels/{}01?transportmode=unicast",userName,password,ip,port,channelNum); + public static String toRtspUrl(String ip, String port, String userName, String password, int channelNum) { + return StrUtil.format("rtsp://{}:{}@{}:{}/Streaming/Channels/{}01?transportmode=unicast", userName, password, ip, port, channelNum); } - - /** - * *

rtsp 回放流地址

* * @param ip ip @@ -610,9 +607,9 @@ public static String toRtspUrl(String ip, String port, String userName, String p * @param endTime 结束时间 格式:yyyy-MM-dd HH:mm:ss * @return {@link String} 返回推流结果 */ - public static String toRtspUrl(String ip,String port,String userName,String password,int channelNum,String beginTime,String endTime){ - return StrUtil.format("rtsp://{}:{}@{}:{}/Streaming/tracks/{}0{}?starttime={}&endtime={}",userName,password,ip,port,channelNum,1, - DateUtil.format(DateUtil.parseDateTime(beginTime),"yyyyMMdd't'HHmmss'z'"),DateUtil.format(DateUtil.parseDateTime(endTime),"yyyyMMdd't'HHmmss'z'")); + public static String toRtspUrl(String ip, String port, String userName, String password, int channelNum, String beginTime, String endTime) { + return StrUtil.format("rtsp://{}:{}@{}:{}/Streaming/tracks/{}0{}?starttime={}&endtime={}", userName, password, ip, port, channelNum, 1, + DateUtil.format(DateUtil.parseDateTime(beginTime), "yyyyMMdd't'HHmmss'z'"), DateUtil.format(DateUtil.parseDateTime(endTime), "yyyyMMdd't'HHmmss'z'")); } @@ -634,9 +631,9 @@ public static void rtspToM3u8(String rtspUrl) { * @param rtspUrl rtsp url * @return {@link Thread} */ - public static void rtspToFlv(String rtspUrl,AsyncContext context){ + public static void rtspToFlv(String rtspUrl, AsyncContext context) { ThreadPoolTaskExecutor taskExecutor = SpringContextHolder.getBean("converterPoolExecutor"); - FlvConverter converter = new FlvConverter(rtspUrl,context); + FlvConverter converter = new FlvConverter(rtspUrl, context); taskExecutor.submit(converter); } @@ -644,13 +641,13 @@ public static void rtspToFlv(String rtspUrl,AsyncContext context){ * 视频码流转flv * * @param outputStream 输出流 - * @param context 上下文 - * @param playHandler 播放句柄 + * @param context 上下文 + * @param playHandler 播放句柄 */ - public static void streamToFlv(PipedInputStream inputStream,PipedOutputStream outputStream, AsyncContext context,Integer playHandler){ + public static void streamToFlv(PipedInputStream inputStream, PipedOutputStream outputStream, AsyncContext context, Integer playHandler) { ThreadPoolTaskExecutor taskExecutor = SpringContextHolder.getBean("converterPoolExecutor"); - FlvConverter converter = new FlvConverter(inputStream,outputStream,context,playHandler); - log.info("线程池主动执行任务的线程的大致数,{}",taskExecutor.getActiveCount()); + FlvConverter converter = new FlvConverter(inputStream, outputStream, context, playHandler); + log.info("线程池主动执行任务的线程的大致数,{}", taskExecutor.getActiveCount()); taskExecutor.submit(converter); } @@ -662,10 +659,10 @@ public static void streamToFlv(PipedInputStream inputStream,PipedOutputStream ou * @param userName 用户名 * @param password 密码 */ - public static void doLoginV40(String ip, String port, String userName, String password){ + public static void doLoginV40(String ip, String port, String userName, String password) { hcNetSDK.NET_DVR_Init(); - hcNetSDK.NET_DVR_SetConnectTime(2000,1); - hcNetSDK.NET_DVR_SetReconnect(10000,true); + hcNetSDK.NET_DVR_SetConnectTime(2000, 1); + hcNetSDK.NET_DVR_SetReconnect(10000, true); HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40(); HCNetSDK.NET_DVR_USER_LOGIN_INFO loginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO(); @@ -683,14 +680,14 @@ public static void doLoginV40(String ip, String port, String userName, String pa // 登录 int lUserID = hcNetSDK.NET_DVR_Login_V40(loginInfo, deviceInfo); - if ( lUserID < 0 ){ - log.error("设备{},注册失败:{}",ip,hcNetSDK.NET_DVR_GetLastError()); + if (lUserID < 0) { + log.error("设备{},注册失败:{}", ip, hcNetSDK.NET_DVR_GetLastError()); throw new RuntimeException("登陆失败"); } - //获取当前SDK状态信息 + //获取当前SDK状态信息 HCNetSDK.NET_DVR_SDKSTATE sdkState = new HCNetSDK.NET_DVR_SDKSTATE(); boolean result = hcNetSDK.NET_DVR_GetSDKState(sdkState); - log.info("登陆成功:{}, 设备状态:{}",lUserID,result); + log.info("登陆成功:{}, 设备状态:{}", lUserID, result); } } diff --git a/src/main/java/cn/nkk/hikvision/utils/Md5Utils.java b/src/main/java/cn/nkk/hikvision/utils/Md5Utils.java index 3bff2da..5a7fe64 100644 --- a/src/main/java/cn/nkk/hikvision/utils/Md5Utils.java +++ b/src/main/java/cn/nkk/hikvision/utils/Md5Utils.java @@ -3,12 +3,12 @@ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; - + /** * Md5工具包 */ public final class Md5Utils { - + /** * Md5加密,返回32的字符串 * @@ -23,8 +23,8 @@ public static String encrypt32(String str) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes("utf-8")); - byte b[] = md.digest(); - int i; + byte b[] = md.digest(); + int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; @@ -44,7 +44,7 @@ public static String encrypt32(String str) { } return result; } - + /** * Md5加密,返回16的字符串(16位的密文就是32位中间的16位) * @@ -57,5 +57,5 @@ public static String encrypt16(String str) { } return encrypt32(str).substring(8, 24); } - + } \ No newline at end of file diff --git a/src/main/java/cn/nkk/hikvision/utils/SpringContextHolder.java b/src/main/java/cn/nkk/hikvision/utils/SpringContextHolder.java index 09d52ff..0640bb0 100644 --- a/src/main/java/cn/nkk/hikvision/utils/SpringContextHolder.java +++ b/src/main/java/cn/nkk/hikvision/utils/SpringContextHolder.java @@ -12,36 +12,23 @@ /** * spring工具类 方便在非spring管理环境中获取bean */ -public final class SpringContextHolder implements BeanFactoryPostProcessor, ApplicationContextAware -{ - /** Spring应用上下文环境 */ +public final class SpringContextHolder implements BeanFactoryPostProcessor, ApplicationContextAware { + /** + * Spring应用上下文环境 + */ private static ConfigurableListableBeanFactory beanFactory; private static ApplicationContext applicationContext; - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException - { - SpringContextHolder.beanFactory = beanFactory; - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException - { - SpringContextHolder.applicationContext = applicationContext; - } - /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws org.springframework.beans.BeansException - * */ @SuppressWarnings("unchecked") - public static T getBean(String name) throws BeansException - { + public static T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } @@ -51,10 +38,8 @@ public static T getBean(String name) throws BeansException * @param clz * @return * @throws org.springframework.beans.BeansException - * */ - public static T getBean(Class clz) throws BeansException - { + public static T getBean(Class clz) throws BeansException { T result = (T) beanFactory.getBean(clz); return result; } @@ -65,8 +50,7 @@ public static T getBean(Class clz) throws BeansException * @param name * @return boolean */ - public static boolean containsBean(String name) - { + public static boolean containsBean(String name) { return beanFactory.containsBean(name); } @@ -76,10 +60,8 @@ public static boolean containsBean(String name) * @param name * @return boolean * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException - * */ - public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException - { + public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return beanFactory.isSingleton(name); } @@ -87,10 +69,8 @@ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionExcept * @param name * @return Class 注册对象的类型 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException - * */ - public static Class getType(String name) throws NoSuchBeanDefinitionException - { + public static Class getType(String name) throws NoSuchBeanDefinitionException { return beanFactory.getType(name); } @@ -100,10 +80,8 @@ public static Class getType(String name) throws NoSuchBeanDefinitionException * @param name * @return * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException - * */ - public static String[] getAliases(String name) throws NoSuchBeanDefinitionException - { + public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return beanFactory.getAliases(name); } @@ -114,8 +92,7 @@ public static String[] getAliases(String name) throws NoSuchBeanDefinitionExcept * @return */ @SuppressWarnings("unchecked") - public static T getAopProxy(T invoker) - { + public static T getAopProxy(T invoker) { return (T) AopContext.currentProxy(); } @@ -124,10 +101,19 @@ public static T getAopProxy(T invoker) * * @return 当前的环境配置 */ - public static String[] getActiveProfiles() - { + public static String[] getActiveProfiles() { return applicationContext.getEnvironment().getActiveProfiles(); } + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + SpringContextHolder.beanFactory = beanFactory; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + SpringContextHolder.applicationContext = applicationContext; + } + }