22 changed files with 1035 additions and 98 deletions
@ -0,0 +1,21 @@ |
|||||
|
package org.nl.acs.opc; |
||||
|
|
||||
|
/** |
||||
|
* @author ldjun |
||||
|
* @version 1.0 |
||||
|
* @date 2023年02月01日 11:28 |
||||
|
* @desc desc |
||||
|
*/ |
||||
|
public class ThreadUtl { |
||||
|
private ThreadUtl() { |
||||
|
} |
||||
|
|
||||
|
public static void sleep(long times) throws RuntimeException { |
||||
|
try { |
||||
|
Thread.sleep(times); |
||||
|
} catch (InterruptedException var3) { |
||||
|
throw new RuntimeException(var3); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,10 @@ |
|||||
|
package org.nl.acs.udw; |
||||
|
|
||||
|
public class UdwConfig { |
||||
|
|
||||
|
//历史记录最大数量
|
||||
|
public static int max_history_length = 10; |
||||
|
|
||||
|
public UdwConfig() { |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
package org.nl.acs.udw; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
@Data |
||||
|
public class UnifiedData { |
||||
|
private Object value; |
||||
|
private Date last_modify_date; |
||||
|
|
||||
|
public UnifiedData() { |
||||
|
this.last_modify_date = new Date(); |
||||
|
} |
||||
|
|
||||
|
public UnifiedData(Object value) { |
||||
|
this.value = value; |
||||
|
this.last_modify_date = new Date(); |
||||
|
} |
||||
|
|
||||
|
public void changeValue(Object value) { |
||||
|
this.value = value; |
||||
|
this.last_modify_date = new Date(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
package org.nl.acs.udw; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface UnifiedDataAccessor { |
||||
|
List<String> getAllKey(); |
||||
|
|
||||
|
Object getValue(String key); |
||||
|
|
||||
|
void setValue(String key, Object value); |
||||
|
|
||||
|
UnifiedData getUnifiedData(String key); |
||||
|
|
||||
|
List<UnifiedData> getHistoryUnifiedData(String key); |
||||
|
|
||||
|
void setValueWithPersistence(String key, Object value); |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package org.nl.acs.udw; |
||||
|
|
||||
|
import org.nl.acs.udw.service.impl.UnifiedDataAccessorImpl; |
||||
|
import org.nl.acs.udw.service.impl.UnifiedDataAppServiceImpl; |
||||
|
|
||||
|
public class UnifiedDataAccessorFactory { |
||||
|
public UnifiedDataAccessorFactory() { |
||||
|
} |
||||
|
|
||||
|
public static UnifiedDataAppService getUnifiedDataAppService() { |
||||
|
return UnifiedDataAppServiceImpl.getInstance(); |
||||
|
} |
||||
|
|
||||
|
public static UnifiedDataAccessor getAccessor(String unified_key) { |
||||
|
UnifiedDataAccessorImpl accessor = new UnifiedDataAccessorImpl(); |
||||
|
accessor.setUnifiedKey(unified_key); |
||||
|
accessor.setUnifiedDataService(getUnifiedDataAppService()); |
||||
|
return accessor; |
||||
|
} |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package org.nl.acs.udw; |
||||
|
|
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import org.nl.acs.udw.service.impl.UnifiedDataUnit; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface UnifiedDataAppService { |
||||
|
/** |
||||
|
* 获取所有的key |
||||
|
* |
||||
|
* @return |
||||
|
*/ |
||||
|
List<String> getAllUnifiedKey(); |
||||
|
|
||||
|
/** |
||||
|
* 根据key获取数据单元 |
||||
|
* |
||||
|
* @param key |
||||
|
* @return |
||||
|
*/ |
||||
|
UnifiedDataUnit getUnifiedDataUnit(String key); |
||||
|
|
||||
|
UnifiedData getUnifiedData(String var1, String var2); |
||||
|
|
||||
|
Object getValue(String var1, String var2); |
||||
|
|
||||
|
void setValue(String var1, String var2, Object var3); |
||||
|
|
||||
|
void setValueNoLog(String var1, String var2, Object var3); |
||||
|
|
||||
|
List<UnifiedData> getHistoryUnifiedData(String var1, String var2); |
||||
|
|
||||
|
List<String> getAllDataKey(String var1); |
||||
|
|
||||
|
void removeValue(String var1, String var2); |
||||
|
|
||||
|
void setValueWithPersistenceNoLog(String var1, String var2, Object var3); |
||||
|
|
||||
|
void setValueWithPersistence(String var1, String var2, Object var3); |
||||
|
|
||||
|
void removeValueWithPersistence(String var1, String var2); |
||||
|
|
||||
|
static boolean isEquals(Object a, Object b) { |
||||
|
return ObjectUtil.equal(a, b); |
||||
|
} |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package org.nl.acs.udw.dto; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* 统一数据源管理 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class UdwDto { |
||||
|
private String unified_key; |
||||
|
private String key; |
||||
|
private Object value; |
||||
|
private Date last_modify_date; |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package org.nl.acs.udw.service; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.nl.acs.udw.dto.UdwDto; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface UdwManageService { |
||||
|
/** |
||||
|
* 根据条件查询 |
||||
|
* |
||||
|
* @param where |
||||
|
* @return |
||||
|
*/ |
||||
|
List<UdwDto> queryByConditions(JSONObject where); |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
/* |
||||
|
* Copyright 2019-2020 Zheng Jie |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.nl.acs.udw.service.impl; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
import org.springframework.http.HttpStatus; |
||||
|
|
||||
|
import static org.springframework.http.HttpStatus.BAD_REQUEST; |
||||
|
|
||||
|
/** |
||||
|
* @author Zheng Jie |
||||
|
* @date 2018-11-23 |
||||
|
* 统一异常处理 |
||||
|
*/ |
||||
|
@Getter |
||||
|
public class BadRequestException extends RuntimeException{ |
||||
|
|
||||
|
private Integer status = BAD_REQUEST.value(); |
||||
|
|
||||
|
public BadRequestException(String msg){ |
||||
|
super(msg); |
||||
|
} |
||||
|
|
||||
|
public BadRequestException(HttpStatus status, String msg){ |
||||
|
super(msg); |
||||
|
this.status = status.value(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package org.nl.acs.udw.service.impl; |
||||
|
|
||||
|
import org.nl.acs.udw.UnifiedData; |
||||
|
import org.nl.acs.udw.UnifiedDataAccessor; |
||||
|
import org.nl.acs.udw.UnifiedDataAppService; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public class UnifiedDataAccessorImpl implements UnifiedDataAccessor { |
||||
|
private String unified_key; |
||||
|
private UnifiedDataAppService unifiedDataAppService; |
||||
|
|
||||
|
public UnifiedDataAccessorImpl() { |
||||
|
} |
||||
|
|
||||
|
public void setUnifiedKey(String unified_key) { |
||||
|
this.unified_key = unified_key; |
||||
|
} |
||||
|
|
||||
|
public void setUnifiedDataService(UnifiedDataAppService unifiedDataService) { |
||||
|
this.unifiedDataAppService = unifiedDataService; |
||||
|
} |
||||
|
|
||||
|
public List<String> getAllKey() { |
||||
|
return this.unifiedDataAppService.getAllDataKey(this.unified_key); |
||||
|
} |
||||
|
|
||||
|
public Object getValue(String key) { |
||||
|
return this.unifiedDataAppService.getValue(this.unified_key, key); |
||||
|
} |
||||
|
|
||||
|
public void setValue(String key, Object value) { |
||||
|
this.unifiedDataAppService.setValue(this.unified_key, key, value); |
||||
|
} |
||||
|
|
||||
|
public void setValueWithPersistence(String key, Object value) { |
||||
|
this.unifiedDataAppService.setValueWithPersistence(this.unified_key, key, value); |
||||
|
} |
||||
|
|
||||
|
public UnifiedData getUnifiedData(String key) { |
||||
|
return this.unifiedDataAppService.getUnifiedData(this.unified_key, key); |
||||
|
} |
||||
|
|
||||
|
public List<UnifiedData> getHistoryUnifiedData(String key) { |
||||
|
return this.unifiedDataAppService.getHistoryUnifiedData(this.unified_key, key); |
||||
|
} |
||||
|
} |
@ -0,0 +1,188 @@ |
|||||
|
package org.nl.acs.udw.service.impl; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.nl.acs.udw.UdwConfig; |
||||
|
import org.nl.acs.udw.UnifiedData; |
||||
|
import org.nl.acs.udw.UnifiedDataAppService; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Slf4j |
||||
|
public class UnifiedDataAppServiceImpl implements UnifiedDataAppService { |
||||
|
public static UnifiedDataAppService unifiedDataAppService; |
||||
|
private Map<String, UnifiedDataUnit> factory = Collections.synchronizedMap(new HashMap()); |
||||
|
|
||||
|
private UnifiedDataAppServiceImpl() { |
||||
|
} |
||||
|
|
||||
|
public static UnifiedDataAppService getInstance() { |
||||
|
if (unifiedDataAppService == null) { |
||||
|
Class var0 = UnifiedDataAppServiceImpl.class; |
||||
|
synchronized (UnifiedDataAppServiceImpl.class) { |
||||
|
if (unifiedDataAppService == null) { |
||||
|
unifiedDataAppService = new UnifiedDataAppServiceImpl(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return unifiedDataAppService; |
||||
|
} |
||||
|
|
||||
|
public List<String> getAllUnifiedKey() { |
||||
|
return new ArrayList(this.factory.keySet()); |
||||
|
} |
||||
|
|
||||
|
public UnifiedDataUnit getUnifiedDataUnit(String unified_key) { |
||||
|
UnifiedDataUnit dataUnit = (UnifiedDataUnit) this.factory.get(unified_key); |
||||
|
return dataUnit == null ? null : dataUnit; |
||||
|
} |
||||
|
|
||||
|
public List<String> getAllDataKey(String unified_key) { |
||||
|
UnifiedDataUnit dataUnit = (UnifiedDataUnit) this.factory.get(unified_key); |
||||
|
if (dataUnit == null) { |
||||
|
return new ArrayList(); |
||||
|
} else { |
||||
|
Map<String, UnifiedData> storage = dataUnit.getStorage(); |
||||
|
return new ArrayList(storage.keySet()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public UnifiedData getUnifiedData(String unified_key, String key) { |
||||
|
UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); |
||||
|
if (dataUnit == null) { |
||||
|
return null; |
||||
|
} else { |
||||
|
Map<String, UnifiedData> storage = dataUnit.getStorage(); |
||||
|
return (UnifiedData) storage.get(key); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Object getValue(String unified_key, String key) { |
||||
|
UnifiedData unifiedData = this.getUnifiedData(unified_key, key); |
||||
|
return unifiedData == null ? null : unifiedData.getValue(); |
||||
|
} |
||||
|
|
||||
|
public void removeValueWithPersistence(String unified_key, String key) { |
||||
|
UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); |
||||
|
if (dataUnit != null) { |
||||
|
Map<String, UnifiedData> storage = dataUnit.getStorage(); |
||||
|
if (storage.containsKey(key)) { |
||||
|
storage.remove(key); |
||||
|
} |
||||
|
|
||||
|
Map<String, List<UnifiedData>> history = dataUnit.getHistory(); |
||||
|
if (history.containsKey(key)) { |
||||
|
history.remove(key); |
||||
|
} |
||||
|
|
||||
|
/*PersistenceService persistenceService = PersistenceServiceFactory.getPersistenceService(); |
||||
|
persistenceService.deleteData(unified_key, key);*/ |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void removeValue(String unified_key, String key) { |
||||
|
UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); |
||||
|
if (dataUnit != null) { |
||||
|
Map<String, UnifiedData> storage = dataUnit.getStorage(); |
||||
|
if (storage.containsKey(key)) { |
||||
|
storage.remove(key); |
||||
|
} |
||||
|
|
||||
|
Map<String, List<UnifiedData>> history = dataUnit.getHistory(); |
||||
|
if (history.containsKey(key)) { |
||||
|
history.remove(key); |
||||
|
} |
||||
|
|
||||
|
if (history.size() == 0) { |
||||
|
this.factory.remove(unified_key); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void setValueNoLog(String unified_key, String key, Object value) { |
||||
|
this.setValue(unified_key, key, value, false, false); |
||||
|
} |
||||
|
|
||||
|
public void setValue(String unified_key, String key, Object value) { |
||||
|
this.setValue(unified_key, key, value, false, true); |
||||
|
} |
||||
|
|
||||
|
public void setValueWithPersistenceNoLog(String unified_key, String key, Object value) { |
||||
|
this.setValue(unified_key, key, value, true, false); |
||||
|
} |
||||
|
|
||||
|
public void setValueWithPersistence(String unified_key, String key, Object value) { |
||||
|
this.setValue(unified_key, key, value, true, true); |
||||
|
} |
||||
|
|
||||
|
public synchronized void setValue(String unified_key, String key, Object value, boolean save, boolean is_log) { |
||||
|
if (unified_key == null) { |
||||
|
throw new BadRequestException(""); |
||||
|
//throw new BusinessException(SystemMessage.cant_be_empty, new Object[]{"unified_key"});
|
||||
|
} else if (key == null) { |
||||
|
throw new BadRequestException(""); |
||||
|
//throw new BusinessException(SystemMessage.cant_be_empty, new Object[]{"key"});
|
||||
|
} else { |
||||
|
if (!this.factory.containsKey(unified_key)) { |
||||
|
this.factory.put(unified_key, new UnifiedDataUnit(unified_key)); |
||||
|
} |
||||
|
|
||||
|
UnifiedDataUnit dataUnit = (UnifiedDataUnit) this.factory.get(unified_key); |
||||
|
Map<String, UnifiedData> storage = dataUnit.getStorage(); |
||||
|
if (!storage.containsKey(key)) { |
||||
|
storage.put(key, new UnifiedData()); |
||||
|
} |
||||
|
|
||||
|
UnifiedData unifiedData = (UnifiedData) storage.get(key); |
||||
|
if (!UnifiedDataAppService.isEquals(unifiedData.getValue(), value)) { |
||||
|
Map<String, List<UnifiedData>> history = dataUnit.getHistory(); |
||||
|
List<UnifiedData> historyunifiedData = (List) history.get(key); |
||||
|
if (historyunifiedData == null) { |
||||
|
history.put(key, new ArrayList()); |
||||
|
} |
||||
|
|
||||
|
UnifiedData historydata = new UnifiedData(); |
||||
|
historydata.setLast_modify_date(unifiedData.getLast_modify_date()); |
||||
|
historydata.setValue(unifiedData.getValue()); |
||||
|
|
||||
|
while (((List) history.get(key)).size() > UdwConfig.max_history_length) { |
||||
|
((List) history.get(key)).remove(UdwConfig.max_history_length); |
||||
|
} |
||||
|
|
||||
|
((List) history.get(key)).add(0, historydata); |
||||
|
Object oldvalue = unifiedData.getValue(); |
||||
|
unifiedData.changeValue(value); |
||||
|
if (save) { |
||||
|
/*PersistenceService persistenceService = PersistenceServiceFactory.getPersistenceService(); |
||||
|
persistenceService.saveData(unified_key, key, StringUtl.getString(value)); |
||||
|
if (is_log) { |
||||
|
this.businessLogger.setResource(unified_key, unified_key); |
||||
|
this.businessLogger.setMaterial(key, key); |
||||
|
this.businessLogger.setContainer(StringUtl.getString(value)); |
||||
|
this.businessLogger.log("统一数据源中: unit: {}, key: {}, 值: {} 更改为 {}。", new Object[]{unified_key, key, oldvalue, value}); |
||||
|
}*/ |
||||
|
} |
||||
|
|
||||
|
if (is_log && key != null && !key.endsWith("heartbeat") && !key.endsWith("distancex") && !key.endsWith("distancey") && !key.endsWith("Xwz") && !key.endsWith("Ywz") && !key.endsWith("Zwz")) { |
||||
|
log.trace("统一数据源中: unit: {}, key: {}, 值: {} 更改为 {}。", new Object[]{unified_key, key, oldvalue, value}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public List<UnifiedData> getHistoryUnifiedData(String unified_key, String key) { |
||||
|
UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); |
||||
|
if (dataUnit == null) { |
||||
|
return new ArrayList(); |
||||
|
} else { |
||||
|
Map<String, List<UnifiedData>> history = dataUnit.getHistory(); |
||||
|
List<UnifiedData> result = (List) history.get(key); |
||||
|
return (List) (result == null ? new ArrayList() : result); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package org.nl.acs.udw.service.impl; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.nl.acs.udw.UnifiedData; |
||||
|
|
||||
|
import java.util.Collections; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Data |
||||
|
public class UnifiedDataUnit { |
||||
|
private String unifiedKey; |
||||
|
private Map<String, UnifiedData> storage = Collections.synchronizedMap(new HashMap()); |
||||
|
private Map<String, List<UnifiedData>> history = Collections.synchronizedMap(new HashMap()); |
||||
|
|
||||
|
public UnifiedDataUnit(String unifiedKey) { |
||||
|
this.unifiedKey = unifiedKey; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
/* |
||||
|
* Copyright 2019-2020 the original author or authors. |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
package org.nl.acs.util; |
||||
|
|
||||
|
/** |
||||
|
* @author: liaojinlong |
||||
|
* @date: 2020/6/9 17:02 |
||||
|
* @since: 1.0 |
||||
|
* @see {@link SpringContextHolder} |
||||
|
* 针对某些初始化方法,在SpringContextHolder 初始化前时,<br> |
||||
|
* 可提交一个 提交回调任务。<br> |
||||
|
* 在SpringContextHolder 初始化后,进行回调使用 |
||||
|
*/ |
||||
|
|
||||
|
public interface CallBack { |
||||
|
/** |
||||
|
* 回调执行方法 |
||||
|
*/ |
||||
|
void executor(); |
||||
|
|
||||
|
/** |
||||
|
* 本回调任务名称 |
||||
|
* @return / |
||||
|
*/ |
||||
|
default String getCallBackName() { |
||||
|
return Thread.currentThread().getId() + ":" + this.getClass().getName(); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,157 @@ |
|||||
|
/* |
||||
|
* Copyright 2019-2020 Zheng Jie |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.nl.acs.util; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.BeansException; |
||||
|
import org.springframework.beans.factory.DisposableBean; |
||||
|
import org.springframework.context.ApplicationContext; |
||||
|
import org.springframework.context.ApplicationContextAware; |
||||
|
import org.springframework.core.env.Environment; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author Jie |
||||
|
* @date 2019-01-07 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class SpringContextHolder implements ApplicationContextAware, DisposableBean { |
||||
|
|
||||
|
private static ApplicationContext applicationContext = null; |
||||
|
//数据库连接的bean名字
|
||||
|
public static String dataSourceBeanName="dataSource"; |
||||
|
private static final List<CallBack> CALL_BACKS = new ArrayList<>(); |
||||
|
private static boolean addCallback = true; |
||||
|
|
||||
|
/** |
||||
|
* 针对 某些初始化方法,在SpringContextHolder 未初始化时 提交回调方法。 |
||||
|
* 在SpringContextHolder 初始化后,进行回调使用 |
||||
|
* |
||||
|
* @param callBack 回调函数 |
||||
|
*/ |
||||
|
public synchronized static void addCallBacks(CallBack callBack) { |
||||
|
if (addCallback) { |
||||
|
SpringContextHolder.CALL_BACKS.add(callBack); |
||||
|
} else { |
||||
|
log.warn("CallBack:{} 已无法添加!立即执行", callBack.getCallBackName()); |
||||
|
callBack.executor(); |
||||
|
} |
||||
|
} |
||||
|
public static ApplicationContext getApplicationContext() { |
||||
|
try { |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return applicationContext; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. |
||||
|
*/ |
||||
|
@SuppressWarnings("unchecked") |
||||
|
public static <T> T getBean(String name) { |
||||
|
assertContextInjected(); |
||||
|
return (T) applicationContext.getBean(name); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. |
||||
|
*/ |
||||
|
public static <T> T getBean(Class<T> requiredType) { |
||||
|
assertContextInjected(); |
||||
|
return applicationContext.getBean(requiredType); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取SpringBoot 配置信息 |
||||
|
* |
||||
|
* @param property 属性key |
||||
|
* @param defaultValue 默认值 |
||||
|
* @param requiredType 返回类型 |
||||
|
* @return / |
||||
|
*/ |
||||
|
public static <T> T getProperties(String property, T defaultValue, Class<T> requiredType) { |
||||
|
T result = defaultValue; |
||||
|
try { |
||||
|
result = getBean(Environment.class).getProperty(property, requiredType); |
||||
|
} catch (Exception ignored) {} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取SpringBoot 配置信息 |
||||
|
* |
||||
|
* @param property 属性key |
||||
|
* @return / |
||||
|
*/ |
||||
|
public static String getProperties(String property) { |
||||
|
return getProperties(property, null, String.class); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取SpringBoot 配置信息 |
||||
|
* |
||||
|
* @param property 属性key |
||||
|
* @param requiredType 返回类型 |
||||
|
* @return / |
||||
|
*/ |
||||
|
public static <T> T getProperties(String property, Class<T> requiredType) { |
||||
|
return getProperties(property, null, requiredType); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 检查ApplicationContext不为空. |
||||
|
*/ |
||||
|
private static void assertContextInjected() { |
||||
|
if (applicationContext == null) { |
||||
|
throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" + |
||||
|
".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 清除SpringContextHolder中的ApplicationContext为Null. |
||||
|
*/ |
||||
|
private static void clearHolder() { |
||||
|
log.debug("清除SpringContextHolder中的ApplicationContext:" |
||||
|
+ applicationContext); |
||||
|
applicationContext = null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void destroy() { |
||||
|
SpringContextHolder.clearHolder(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
||||
|
if (SpringContextHolder.applicationContext != null) { |
||||
|
log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext); |
||||
|
} |
||||
|
SpringContextHolder.applicationContext = applicationContext; |
||||
|
if (addCallback) { |
||||
|
for (CallBack callBack : SpringContextHolder.CALL_BACKS) { |
||||
|
callBack.executor(); |
||||
|
} |
||||
|
CALL_BACKS.clear(); |
||||
|
} |
||||
|
SpringContextHolder.addCallback = false; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue