Commit 600d4ff0 authored by yanzg's avatar yanzg

修改实例化关系

parent e1f8e676
......@@ -35,7 +35,7 @@
<fastjson.version>1.2.62</fastjson.version>
<mysql.version>6.0.6</mysql.version>
<poi.version>3.9</poi.version>
<yzg.version>1.0-SNAPSHOT</yzg.version>
<yzg.version>1.1-SNAPSHOT</yzg.version>
<swagger.version>2.9.2</swagger.version>
<guava.version>20.0</guava.version>
<!--<jetcache.version>2.6.0</jetcache.version>-->
......
package com.yanzuoguang.cloud;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 整个模块的所有配置信息
*
* @author 颜佐光
*/
@Component
public class CloudConfig {
@Value("${spring.application.name}")
protected String applicationName;
@Value("${yzg.logCommon:false}")
protected boolean logCommon;
@Value("${yzg.logAll:false}")
protected boolean logAll = false;
@Value("${yzg.log.base:false}")
private boolean logBase;
@Value("${yzg.cacheTime:120}")
protected int cacheTime = 120;
@Value("${yzg.reqSize:5000}")
private int reqSize = 5000;
/**
* 1个请求最长时间(毫秒)
*/
@Value("${yzg.log.time.max:10000}")
private int logTimeMax;
/**
* 1个请求间隔记录时间(毫秒)
*/
@Value("${yzg.log.time.split:5000}")
private int logTimeSplit;
/**
* 日志不需要过滤的地址
*/
@Value("${yzg.log.logNotFilter:^.*login.*$}")
private String logNotFilter;
/**
* 日志需要过滤的地址
*/
@Value("${yzg.log.logFilter:^.*(log|yzg).*$}")
private String logFilter;
/**
* 服务器存储地址,注意权限
*/
@Value("${yzg.upload.server:}")
private String serverUrl;
/**
* 外网地址
*/
@Value("${yzg.upload.display:}")
private String displayUrl;
@Value("${yzg.gateway:^.*gateway.*$}")
private String gateWay;
public String getApplicationName() {
return applicationName;
}
public boolean isLogCommon() {
return logCommon;
}
public boolean isLogAll() {
return logAll;
}
public boolean isLogBase() {
return logBase;
}
public int getCacheTime() {
return cacheTime;
}
public int getReqSize() {
return reqSize;
}
public int getLogTimeMax() {
return logTimeMax;
}
public int getLogTimeSplit() {
return logTimeSplit;
}
public String getLogNotFilter() {
return logNotFilter;
}
public String getLogFilter() {
return logFilter;
}
public String getGateWay() {
return gateWay;
}
public String getServerUrl() {
if (StringHelper.isEmpty(this.serverUrl)) {
throw new CodeException("请配置yzg.upload.server");
}
return serverUrl;
}
public String getDisplayUrl() {
if (StringHelper.isEmpty(this.serverUrl)) {
throw new CodeException("请配置yzg.upload.display");
}
return displayUrl;
}
/**
* 是否属于网关服务,网关服务不进行监控
*
* @return
*/
public boolean isGateWay() {
return this.applicationName.toLowerCase().matches(this.gateWay);
}
}
package com.yanzuoguang.cloud.aop;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.cloud.vo.YzgFileUploadReqVo;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.ArrayHelper;
......@@ -13,9 +15,11 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -34,28 +38,34 @@ import java.util.List;
*
* @author 颜佐光
*/
public class BaseRequestAspect {
public class BaseRequestAspect implements ApplicationContextAware {
@Value("${spring.application.name}")
protected String applicationName;
protected LogLocal logLocal;
@Value("${yzg.logCommon:false}")
protected boolean logCommon;
@Value("${yzg.logAll:false}")
protected boolean logAll = false;
@Value("${yzg.cacheTime:120}")
protected int cacheTime = 120;
@Value("${yzg.reqSize:5000}")
private int reqSize = 5000;
@Autowired
private LogLocal logLocal;
protected CloudConfig cloudConfig;
private ResponseResult responseDefault = new ResponseResult();
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.logLocal = applicationContext.getBean(LogLocal.class);
this.cloudConfig = applicationContext.getBean(CloudConfig.class);
}
/**
* 开始记录日志对象
*
......@@ -65,7 +75,7 @@ public class BaseRequestAspect {
* @return
*/
protected LogVo startLog(String actionKey, String url, String body) {
return this.logLocal.startLog(actionKey, String.format("%s:%s", this.applicationName, url), body);
return this.logLocal.startLog(actionKey, String.format("%s:%s", this.cloudConfig.getApplicationName(), url), body);
}
/**
......@@ -144,8 +154,8 @@ public class BaseRequestAspect {
* @return
*/
private String getMaxString(String paraJson) {
if (paraJson != null && paraJson.length() > reqSize) {
paraJson = paraJson.substring(0, reqSize);
if (paraJson != null && paraJson.length() > this.cloudConfig.getReqSize()) {
paraJson = paraJson.substring(0, this.cloudConfig.getReqSize());
}
return paraJson;
}
......@@ -175,7 +185,7 @@ public class BaseRequestAspect {
protected long requestLog(Class<?> cls, String tag, String url, String requestBody, boolean logFlag) {
long start = System.currentTimeMillis();
try {
boolean isLog = logFlag && logCommon;
boolean isLog = logFlag && this.cloudConfig.isLogCommon();
if (isLog) {
Log.info(cls, " %s [ %s ] request: %s", tag, url, this.getMaxString(requestBody));
}
......@@ -209,7 +219,7 @@ public class BaseRequestAspect {
log.setContentTo(requestBody);
}
long time = System.currentTimeMillis() - start;
boolean isLogDisplay = (logFlag && logCommon) || resultEx != null;
boolean isLogDisplay = (logFlag && this.cloudConfig.isLogCommon()) || resultEx != null;
boolean isLogDatabase = log != null || resultEx != null;
// 处理结果
ResponseResult responseResult = responseDefault;
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import com.yanzuoguang.cloud.aop.log.LogFeignBase;
/**
* 调用外置服务保存日志对象
*
* @author 颜佐光
*/
public interface LogFeign {
/**
* 保存日志对象
*
* @param log 需要保存的日志对象
* @return 日志服务返回结果
*/
ResponseResult<String> save(LogVo log);
public interface LogFeign extends LogFeignBase {
}
......@@ -14,9 +14,10 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
......@@ -34,19 +35,42 @@ public class WebAspect extends BaseRequestAspect {
private static final String TAG = WebAspect.class.getSimpleName();
@Autowired
private TokenServiceCall tokenServiceCall;
@Value("${yzg.gateway:^.*gateway.*$}")
private String gateWay;
@Autowired
private ApplicationContext context;
/**
* 是否存在初始化方法
*/
private WebAssecptInit webAssecptInit;
private WebAspectInit webAspectInit;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
super.setApplicationContext(applicationContext);
this.tokenServiceCall = applicationContext.getBean(TokenServiceCall.class);
try {
this.webAspectInit = applicationContext.getBean(WebAspectInit.class);
} catch (Exception ex) {
webAspectInit = new WebAspectInit() {
@Override
public void init(Object req) {
}
};
Log.error(WebAspect.class, "请设置登录默认处理函数");
Log.error(WebAspect.class, ex);
}
}
/**
* exec aop point aspect
......@@ -55,7 +79,6 @@ public class WebAspect extends BaseRequestAspect {
public void webAspect() {
}
/**
* 执行环形切面
*
......@@ -65,7 +88,7 @@ public class WebAspect extends BaseRequestAspect {
@Around(value = "webAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
boolean isGateWay = isGateWay();
boolean isGateWay = cloudConfig.isGateWay();
if (isGateWay) {
result = executeMethod(joinPoint);
return result;
......@@ -82,24 +105,11 @@ public class WebAspect extends BaseRequestAspect {
Exception ex = null;
boolean isInit = false;
try {
// 请求登录服务初始化
isInit = tokenServiceCall.tokenInit();
if (webAssecptInit == null) {
try {
webAssecptInit = context.getBean(WebAssecptInit.class);
} catch (Exception e) {
webAssecptInit = new WebAssecptInit() {
@Override
public void init(Object req) {
}
};
Log.error(WebAspect.class, "请设置登录默认处理函数");
Log.error(WebAspect.class, e);
}
}
if (webAssecptInit != null) {
// 初始化请求参数
for (Object arg : joinPoint.getArgs()) {
webAssecptInit.init(arg);
}
webAspectInit.init(arg);
}
result = executeMethod(joinPoint);
return result;
......@@ -116,10 +126,13 @@ public class WebAspect extends BaseRequestAspect {
throw e;
}
} finally {
// 登录服务最终处理
tokenServiceCall.tokenFinish();
// 登录服务已经初始化则删除登录日志
if (isInit) {
TokenHelper.remove();
}
// 获取最终的处理结果,写入到日志
try {
requestBody = getRequestBody(joinPoint);
} catch (Exception e) {
......@@ -128,15 +141,6 @@ public class WebAspect extends BaseRequestAspect {
}
}
/**
* 是否属于网关服务,网关服务不进行监控
*
* @return
*/
private boolean isGateWay() {
return this.applicationName.toLowerCase().matches(this.gateWay);
}
/**
* 获取返回的至类型
......
package com.yanzuoguang.cloud.aop;
public interface WebAssecptInit {
/**
*
* @author 颜佐光
*/
public interface WebAspectInit {
/**
* 对请求的数据初始化
......
package com.yanzuoguang.cloud.aop;
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.LogFeign;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
/**
......@@ -16,19 +18,44 @@ import java.util.concurrent.LinkedBlockingQueue;
* @author 颜佐光
*/
@Component
public class LogBase implements ThreadNext.Next, InitializingBean {
public class LogBase implements ThreadNext.Next, InitializingBean, ApplicationContextAware {
/**
* 缓存队列
*/
protected volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<>();
@Autowired
protected List<LogFeign> logFeigns;
private CloudConfig cloudConfig;
private LogFeign logFeign;
private LogFeignBase logFeign;
@Value("${yzg.log.base:false}")
private boolean logBase;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.cloudConfig = applicationContext.getBean(CloudConfig.class);
// 获取个人实例话的对象
try {
this.logFeign = applicationContext.getBean(LogFeign.class);
} catch (Exception ex) {
System.err.println("请添加处理日志服务,实现LogFeign接口!");
}
// 默认实例话对象
if (this.logFeign == null) {
this.logFeign = applicationContext.getBean(LogFeignBase.class);
}
}
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
......@@ -42,8 +69,11 @@ public class LogBase implements ThreadNext.Next, InitializingBean {
*/
@Override
public void afterPropertiesSet() throws Exception {
// 判断是否要记录日志
if (cloudConfig.isLogBase()) {
ThreadNext.start(this, "save log error");
}
}
/**
* 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
......@@ -51,6 +81,10 @@ public class LogBase implements ThreadNext.Next, InitializingBean {
* @param logVo
*/
public void addLog(LogVo logVo) {
// 当不记录日志时,则直接忽略
if (!cloudConfig.isLogBase()) {
return;
}
cache.add(logVo);
}
......@@ -61,35 +95,25 @@ public class LogBase implements ThreadNext.Next, InitializingBean {
* @throws Exception 异常信息
*/
@Override
public boolean next() throws Exception {
if (!logBase) {
cache.clear();
public boolean next() {
if (cache.isEmpty()) {
return true;
}
if (logFeign == null) {
for (LogFeign log : logFeigns) {
if (log instanceof LogFeignDefault) {
if (logFeign == null) {
logFeign = log;
}
} else {
logFeign = log;
}
}
}
// 循环处理日志信息
while (cache.size() > 0) {
// 取出一条日志
LogVo item = cache.poll();
if (item != null) {
// 判断日志信息是否为空
if (item == null) {
continue;
}
// 保存日志
try {
logFeign.save(item);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return true;
}
......
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
/**
* 调用外置服务保存日志对象
*
* @author 颜佐光
*/
public interface LogFeignBase {
/**
* 保存日志对象
*
* @param log 需要保存的日志对象
* @return 日志服务返回结果
*/
ResponseResult<String> save(LogVo log);
}
package com.yanzuoguang.cloud.aop;
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
......@@ -10,9 +10,7 @@ import org.springframework.stereotype.Component;
* @author 颜佐光
*/
@Component
public class LogFeignDefault implements LogFeign {
private boolean init = false;
public class LogFeignDefault implements LogFeignBase {
/**
* 保存日志对象
......@@ -22,10 +20,6 @@ public class LogFeignDefault implements LogFeign {
*/
@Override
public ResponseResult<String> save(LogVo log) {
if (!init) {
init = true;
System.err.println("请添加处理日志服务,实现LogFeign接口!");
}
return ResponseResult.result("处理成功");
}
}
package com.yanzuoguang.cloud.aop;
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.Timeout;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 当前线程日志编写
......@@ -17,41 +24,42 @@ import java.util.*;
* @author 颜佐光
*/
@Component
public class LogLocal implements ThreadNext.Next, InitializingBean {
public class LogLocal implements ThreadNext.Next, InitializingBean, ApplicationContextAware {
/**
* 超时状态
*/
public static final String MAX_TIME = "MAX_TIME";
public static final String MAX_TIME_NAME = "执行超时";
/**
* 缓存队列
*/
protected volatile Map<String, Timeout<LogVo>> cache = new Hashtable<>();
protected volatile Map<String, Timeout<LogVo>> cache = new ConcurrentHashMap<>();
/**
* 日志基础
*/
@Autowired
private LogBase logBase;
@Value("${spring.application.name}")
protected String applicationName;
/**
* 1个请求最长时间(毫秒)
*/
@Value("${yzg.log.time.max:10000}")
private int logTimeMax;
/**
* 1个请求间隔记录时间(毫秒)
*/
@Value("${yzg.log.time.split:5000}")
private int logTimeSplit;
@Value("${yzg.log.notFilter:^.*login.*$}")
private String notFilter;
@Value("${yzg.log.filter:^.*(log|yzg).*$}")
private String filter;
private CloudConfig cloudConfig;
/**
* 超时状态
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
public static final String MAX_TIME = "MAX_TIME";
public static final String MAX_TIME_NAME = "执行超时";
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
logBase = applicationContext.getBean(LogBase.class);
cloudConfig = applicationContext.getBean(CloudConfig.class);
}
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
......@@ -163,7 +171,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
*/
public boolean isLog(String... keys) {
List<String> list = new ArrayList<>();
list.add(this.applicationName);
list.add(this.cloudConfig.getApplicationName());
for (String item : keys) {
list.add(item);
}
......@@ -173,8 +181,8 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
boolean filter = false;
for (String item : list) {
String lower = item.toLowerCase();
notFilter = notFilter || lower.matches(this.notFilter);
filter = filter || lower.matches(this.filter);
notFilter = notFilter || lower.matches(this.cloudConfig.getLogNotFilter());
filter = filter || lower.matches(this.cloudConfig.getLogFilter());
}
if (notFilter) {
return false;
......@@ -194,7 +202,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
keys.addAll(cache.keySet());
for (String key : keys) {
Timeout<LogVo> timeout = cache.get(key);
if (timeout == null || !timeout.isMaxTime(this.logTimeMax, this.logTimeSplit)) {
if (timeout == null || !timeout.isMaxTime(this.cloudConfig.getLogTimeMax(), this.cloudConfig.getLogTimeSplit())) {
continue;
}
writeTimeout(timeout);
......
package com.yanzuoguang.cloud.file;
import com.yanzuoguang.util.vo.file.YzgFileBaseVo;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.beans.factory.annotation.Value;
import com.yanzuoguang.util.vo.file.YzgFileBaseVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.io.File;
/**
* 文件配置对象
* 颜佐光上传文件服务文件配置对象
*
* @author 颜佐光
*/
@Component
public class YzgFileConfig {
public class YzgFileConfig implements ApplicationContextAware {
/**
* 服务器存储地址,注意权限
*/
@Value("${yzg.upload.server:}")
private String serverUrl;
private CloudConfig cloudConfig;
/**
* 外网地址
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Value("${yzg.upload.display:}")
private String displayUrl;
public String getServerUrl() {
if (StringHelper.isEmpty(this.serverUrl)) {
throw new CodeException("请配置yzg.upload.server");
}
return serverUrl;
}
public String getDisplayUrl() {
if (StringHelper.isEmpty(this.serverUrl)) {
throw new CodeException("请配置yzg.upload.display");
}
return displayUrl;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.cloudConfig = applicationContext.getBean(CloudConfig.class);
}
/**
......@@ -49,7 +49,7 @@ public class YzgFileConfig {
*/
public YzgFileBaseVo init(String serverFile) {
YzgFileBaseVo item = new YzgFileBaseVo();
if(!StringHelper.isEmpty(serverFile)){
if (!StringHelper.isEmpty(serverFile)) {
init(item, serverFile);
}
return item;
......@@ -66,9 +66,9 @@ public class YzgFileConfig {
// 服务器相对路径
item.setServer(fromUrl);
// 对外显示全路径
item.setDisplay(getUrl(this.getDisplayUrl(), fromUrl));
item.setDisplay(getUrl(this.cloudConfig.getDisplayUrl(), fromUrl));
// 文件全路径
String serverFullPath = getUrl(this.getServerUrl(), fromUrl);
String serverFullPath = getUrl(this.cloudConfig.getServerUrl(), fromUrl);
// 获取文件大小
File file = new File(serverFullPath);
if (file.exists()) {
......@@ -86,8 +86,8 @@ public class YzgFileConfig {
* @return
*/
public String getServerPath(String fromUrl) {
String serverUrl = this.getServerUrl();
String displayUrl = this.getDisplayUrl();
String serverUrl = this.cloudConfig.getServerUrl();
String displayUrl = this.cloudConfig.getDisplayUrl();
// 处理serverFile为简写
if (fromUrl.startsWith(serverUrl)) {
fromUrl = fromUrl.substring(serverUrl.length());
......@@ -106,7 +106,7 @@ public class YzgFileConfig {
*/
public String getServerFullPath(String fromUrl) {
fromUrl = getServerPath(fromUrl);
return getUrl(this.getServerUrl(), fromUrl);
return getUrl(this.cloudConfig.getServerUrl(), fromUrl);
}
/**
......@@ -117,7 +117,7 @@ public class YzgFileConfig {
*/
public String getDisplayPath(String fromUrl) {
fromUrl = getServerPath(fromUrl);
return getUrl(this.getDisplayUrl(), fromUrl);
return getUrl(this.cloudConfig.getDisplayUrl(), fromUrl);
}
private String getUrl(String folder, String fromUrl) {
......
......@@ -6,8 +6,10 @@ import com.yanzuoguang.token.TokenLoad;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
/**
......@@ -16,34 +18,36 @@ import org.springframework.stereotype.Component;
* @author 颜佐光
*/
@Component
public class TokenServiceCall implements TokenLoad {
@Autowired
private ApplicationContext context;
public class TokenServiceCall implements TokenLoad, ApplicationContextAware {
private TokenService tokenService = null;
private int flagCount = 1;
private MemoryCache<Integer> countCache = new MemoryCache<>();
public TokenServiceCall() {
TokenHelper.setTokenLoad(this);
}
private void init() {
if (tokenService != null) {
return;
}
if (flagCount < 1) {
return;
}
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
try {
tokenService = context.getBean(TokenService.class);
tokenService = applicationContext.getBean(TokenService.class);
} catch (Exception ex) {
Log.error(TokenServiceCall.class, "获取登录服务失败:" + ex.getMessage(), ex);
} finally {
flagCount--;
}
}
......@@ -51,7 +55,6 @@ public class TokenServiceCall implements TokenLoad {
* 请求时初始化token
*/
public boolean tokenInit() {
init();
if (tokenService == null) {
return false;
}
......@@ -66,7 +69,6 @@ public class TokenServiceCall implements TokenLoad {
* 请求时结束token
*/
public void tokenFinish() {
init();
if (tokenService == null) {
return;
}
......@@ -83,7 +85,6 @@ public class TokenServiceCall implements TokenLoad {
*/
@Override
public TokenData load(String token) {
init();
if (tokenService == null) {
return null;
}
......
......@@ -2,6 +2,7 @@ package com.yanzuoguang.code;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.helper.ByteHelper;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
......@@ -13,6 +14,7 @@ import java.util.Random;
*
* @author 颜佐光
*/
@Component
public class CodePwdImpl implements CodePwd {
/**
......
......@@ -28,11 +28,4 @@ public @interface TableAnnotation {
* @return
*/
int type() default DaoConst.FIELD_NONE;
/**
* 包含的Sql语句,会根据SQL语句名称替换默认的SQL语句
*
* @return
*/
Class[] sql() default {};
}
package com.yanzuoguang.extend;
package com.yanzuoguang.db;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 打印SQL日志
*
* @author 颜佐光
*/
@Component
......@@ -19,7 +24,6 @@ public class ConfigDb {
*/
@Value("${yzg.PrintSql:false}")
private boolean printSql;
/**
* 判断是否匹配过滤
*/
......@@ -29,6 +33,10 @@ public class ConfigDb {
@Value("${yzg.row.name.type:0}")
private int rowNameType;
@Resource
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbc;
public boolean isPrintMapper() {
return printMapper;
}
......@@ -44,4 +52,8 @@ public class ConfigDb {
public int getRowNameType() {
return rowNameType;
}
public JdbcTemplate getJdbc() {
return jdbc;
}
}
......@@ -2,7 +2,7 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.dao.TableAnnotation;
import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.util.base.MethodField;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper;
......
package com.yanzuoguang.db.impl;
import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.util.vo.MapRow;
import com.yanzuoguang.util.vo.Ref;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
......@@ -22,18 +24,32 @@ import java.util.List;
* @author 颜佐光
*/
@Component
public class DbExecuteImpl implements DbExecute {
public class DbExecuteImpl implements DbExecute, ApplicationContextAware {
@Resource
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbc;
@Resource
private DbPrintSql printSql;
@Resource
private ConfigDb configDb;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.printSql = applicationContext.getBean(DbPrintSql.class);
this.configDb = applicationContext.getBean(ConfigDb.class);
}
/**
* 更新SQL语句的执行
*
......@@ -49,7 +65,7 @@ public class DbExecuteImpl implements DbExecute {
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
row = jdbc.update(sql, paras);
row = this.getJdbc().update(sql, paras);
return row;
} finally {
printSql.print(targetClass, sqlName, start, row, sql, paras);
......@@ -83,8 +99,7 @@ public class DbExecuteImpl implements DbExecute {
row.value++;
}
};
jdbc.query(sql, rowCallbackHandler, paras);
this.getJdbc().query(sql, rowCallbackHandler, paras);
} finally {
printSql.print(targetClass, sqlName, start, row.value, sql, paras);
}
......@@ -108,7 +123,7 @@ public class DbExecuteImpl implements DbExecute {
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
List<T> ret = jdbc.query(sql, paras, AllBeanRowMapper.getInstance(cls, configDb));
List<T> ret = this.getJdbc().query(sql, paras, AllBeanRowMapper.getInstance(cls, configDb));
if (ret == null) {
ret = new ArrayList<T>();
}
......@@ -148,7 +163,7 @@ public class DbExecuteImpl implements DbExecute {
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
SqlRowSet rowSet = jdbc.queryForRowSet(sql, paras);
SqlRowSet rowSet = this.getJdbc().queryForRowSet(sql, paras);
while (rowSet.next()) {
row = 1;
return rowSet.getObject(1);
......@@ -175,4 +190,7 @@ public class DbExecuteImpl implements DbExecute {
.replaceAll("(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?", "");
}
private JdbcTemplate getJdbc() {
return configDb.getJdbc();
}
}
package com.yanzuoguang.db.impl;
import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import org.springframework.stereotype.Component;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment