Skip to content

Commit

Permalink
Merge pull request #76 from mouzt/bugfix/lazy-opt
Browse files Browse the repository at this point in the history
@lazy 问题
  • Loading branch information
mouzt authored Aug 8, 2022
2 parents bf794c7 + 6cb4dde commit 6322a6c
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 37 deletions.
2 changes: 1 addition & 1 deletion bizlog-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bizlog-sdk</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0</version>
<parent>
<groupId>io.github.mouzt</groupId>
<artifactId>mzt-biz-log</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* !不要删掉,为 null 就不代理了哦
* true 都使用 CGLIB 代理
* false 目标对象实现了接口 – 使用JDK动态代理机制(代理所有实现了的接口) 目标对象没有接口(只有实现类) – 使用CGLIB代理机制
*
* @return 不强制 cglib
*/
boolean proxyTargetClass() default false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@
@Documented
public @interface LogRecord {
/**
* 方法执行成功后的日志模版
* @return 方法执行成功后的日志模版
*/
String success();

/**
* 方法执行失败后的日志模版
* @return 方法执行失败后的日志模版
*/
String fail() default "";

/**
* 日志的操作人
* @return 日志的操作人
*/
String operator() default "";

/**
* 操作日志的类型,比如:订单类型、商品类型
* @return 操作日志的类型,比如:订单类型、商品类型
*/
String type();

/**
* 日志的子类型,比如订单的C端日志,和订单的B端日志,type都是订单类型,但是子类型不一样
* @return 日志的子类型,比如订单的C端日志,和订单的B端日志,type都是订单类型,但是子类型不一样
*/
String subType() default "";

/**
* 日志绑定的业务标识
* @return 日志绑定的业务标识
*/
String bizNo();

/**
* 日志的额外信息
* @return 日志的额外信息
*/
String extra() default "";

/**
* @return 是否记录日志
*/
String condition() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.mzt.logapi.starter.support.aop.BeanFactoryLogRecordAdvisor;
import com.mzt.logapi.starter.support.aop.LogRecordInterceptor;
import com.mzt.logapi.starter.support.aop.LogRecordOperationSource;
import com.mzt.logapi.starter.support.parse.LogFunctionParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
Expand Down Expand Up @@ -61,11 +60,11 @@ public DefaultParseFunction parseFunction() {

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryLogRecordAdvisor logRecordAdvisor(IFunctionService functionService, DiffParseFunction diffParseFunction) {
public BeanFactoryLogRecordAdvisor logRecordAdvisor() {
BeanFactoryLogRecordAdvisor advisor =
new BeanFactoryLogRecordAdvisor();
advisor.setLogRecordOperationSource(logRecordOperationSource());
advisor.setAdvice(logRecordInterceptor(functionService, diffParseFunction));
advisor.setAdvice(logRecordInterceptor());
return advisor;
}

Expand All @@ -77,20 +76,20 @@ public ILogRecordPerformanceMonitor logRecordPerformanceMonitor() {

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public LogRecordInterceptor logRecordInterceptor(IFunctionService functionService, DiffParseFunction diffParseFunction) {
public LogRecordInterceptor logRecordInterceptor() {
LogRecordInterceptor interceptor = new LogRecordInterceptor();
interceptor.setLogRecordOperationSource(logRecordOperationSource());
interceptor.setTenant(enableLogRecord.getString("tenant"));
interceptor.setLogFunctionParser(logFunctionParser(functionService));
interceptor.setDiffParseFunction(diffParseFunction);
//interceptor.setLogFunctionParser(logFunctionParser(functionService));
//interceptor.setDiffParseFunction(diffParseFunction);
interceptor.setLogRecordPerformanceMonitor(logRecordPerformanceMonitor());
return interceptor;
}

@Bean
public LogFunctionParser logFunctionParser(IFunctionService functionService) {
return new LogFunctionParser(functionService);
}
// @Bean
// public LogFunctionParser logFunctionParser(IFunctionService functionService) {
// return new LogFunctionParser(functionService);
// }

@Bean
public DiffParseFunction diffParseFunction(IDiffItemsToLogContentService diffItemsToLogContentService) {
Expand All @@ -102,8 +101,8 @@ public DiffParseFunction diffParseFunction(IDiffItemsToLogContentService diffIte
@Bean
@ConditionalOnMissingBean(IDiffItemsToLogContentService.class)
@Role(BeanDefinition.ROLE_APPLICATION)
public IDiffItemsToLogContentService diffItemsToLogContentService(IFunctionService functionService, LogRecordProperties logRecordProperties) {
return new DefaultDiffItemsToLogContentService(functionService, logRecordProperties);
public IDiffItemsToLogContentService diffItemsToLogContentService(LogRecordProperties logRecordProperties) {
return new DefaultDiffItemsToLogContentService(logRecordProperties);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import com.mzt.logapi.starter.annotation.DiffLogField;
import com.mzt.logapi.starter.configuration.LogRecordProperties;
import de.danielbechler.diff.node.DiffNode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

Expand All @@ -18,11 +24,17 @@
* create on 2022/1/3 8:52 下午
*/
@Slf4j
@AllArgsConstructor
public class DefaultDiffItemsToLogContentService implements IDiffItemsToLogContentService {
@Setter
@Getter
public class DefaultDiffItemsToLogContentService implements IDiffItemsToLogContentService, BeanFactoryAware, SmartInitializingSingleton {

private final IFunctionService functionService;
private IFunctionService functionService;
private final LogRecordProperties logRecordProperties;
private BeanFactory beanFactory;

public DefaultDiffItemsToLogContentService(LogRecordProperties logRecordProperties) {
this.logRecordProperties = logRecordProperties;
}

@Override
public String toLogContent(DiffNode diffNode, final Object sourceObject, final Object targetObject) {
Expand Down Expand Up @@ -154,4 +166,14 @@ private String getFunctionValue(Object canonicalGet, String functionName) {
private Object getFieldValue(DiffNode node, Object o2) {
return node.canonicalGet(o2);
}

@Override
public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}

@Override
public void afterSingletonsInstantiated() {
this.functionService = beanFactory.getBean(IFunctionService.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
* @author mzt.
*/
public class LogRecordConfigureSelector extends AdviceModeImportSelector<EnableLogRecord> {
private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
"com.mzt.logapi.starter.configuration.LogRecordProxyAutoConfiguration";


@Override
@Nullable
Expand All @@ -24,7 +21,7 @@ public String[] selectImports(AdviceMode adviceMode) {
case PROXY:
return new String[]{AutoProxyRegistrar.class.getName(), LogRecordProxyAutoConfiguration.class.getName()};
case ASPECTJ:
return new String[]{ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
return new String[]{LogRecordProxyAutoConfiguration.class.toString()};
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import com.mzt.logapi.beans.LogRecord;
import com.mzt.logapi.beans.LogRecordOps;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.service.IFunctionService;
import com.mzt.logapi.service.ILogRecordPerformanceMonitor;
import com.mzt.logapi.service.ILogRecordService;
import com.mzt.logapi.service.IOperatorGetService;
import com.mzt.logapi.service.impl.DiffParseFunction;
import com.mzt.logapi.starter.support.parse.LogFunctionParser;
import com.mzt.logapi.starter.support.parse.LogRecordValueParser;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -18,7 +21,7 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
Expand All @@ -35,7 +38,7 @@
* @author mzt.
*/
@Slf4j
public class LogRecordInterceptor extends LogRecordValueParser implements InitializingBean, MethodInterceptor, Serializable {
public class LogRecordInterceptor extends LogRecordValueParser implements MethodInterceptor, Serializable, SmartInitializingSingleton {

private LogRecordOperationSource logRecordOperationSource;

Expand Down Expand Up @@ -224,14 +227,12 @@ public void setLogRecordPerformanceMonitor(ILogRecordPerformanceMonitor logRecor
}

@Override
public void afterPropertiesSet() throws Exception {
public void afterSingletonsInstantiated() {
bizLogService = beanFactory.getBean(ILogRecordService.class);
operatorGetService = beanFactory.getBean(IOperatorGetService.class);
Preconditions.checkNotNull(bizLogService, "bizLogService not null");
}
this.setLogFunctionParser(new LogFunctionParser(beanFactory.getBean(IFunctionService.class)));
this.setDiffParseFunction(beanFactory.getBean(DiffParseFunction.class));

public void setOperatorGetService(IOperatorGetService operatorGetService) {
this.operatorGetService = operatorGetService;
}

@Data
Expand Down
2 changes: 1 addition & 1 deletion bizlog-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>io.github.mouzt</groupId>
<artifactId>bizlog-sdk</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0</version>
</dependency>

<!--druid 依赖-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//package com.mzt.logserver.aop;
//
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.stereotype.Component;
//
///**
// * @author muzhantong
// * create on 2022/8/7 12:01 AM
// */
//@Aspect
//@Component
//public class UserBeanAspect {
//
// @Pointcut(value = "execution(* com.mzt.logserver.UserQueryService.*(..))")
// private void myPointCut() {
// }
//
// @Around(value = "myPointCut()")
// public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// System.out.println("before");
// Object obj = joinPoint.proceed();
// System.out.println("after");
// return obj;
//
// }
//}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.mzt.logserver.function;

import com.mzt.logapi.service.IParseFunction;
import com.mzt.logserver.UserQueryService;
import com.mzt.logserver.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
* @author muzhantong
* create on 2021/2/9 5:20 下午
Expand All @@ -14,6 +17,10 @@
@Component
public class OrderBeforeParseFunction implements IParseFunction {

@Resource
// @Lazy
private UserQueryService userQueryService;

@Override
public boolean executeBefore() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mzt.logapi.starter.annotation.LogRecord;
import com.mzt.logserver.UserQueryService;
import com.mzt.logserver.infrastructure.constants.LogRecordType;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

Expand All @@ -14,6 +15,7 @@
* create on 2021/2/10 11:13 上午
*/
@Service
@Slf4j
public class UserQueryServiceImpl implements UserQueryService {

@Override
Expand Down
1 change: 1 addition & 0 deletions bizlog-server/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ spring:
logging:
level:
com.mzt.logserver.repository.mapper: debug
# org.springframework.aop.framework.autoproxy: trace
com.mzt.logapi.service.impl: debug
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
| 2.0.0 | 1.修改了@LogRecordAnnotation 注解的名字 到LogRecord,2. 修改了注解的一些属性,使属性名称使得含义更加清晰更加,3.增加了默认的server端的实现 |
| 2.0.1 | 修复了接口上的注解不能被拦截的问题 |
| 2.0.2 | 1.修复了 LogFunctionParser 的NPE,2. 注解上添加了ElementType.TYPE,3.记录了当前执行方法的Class和Method 4. 重新fix了没有加EnableTransactionManagement 切面不生效的逻辑 5. 增加了 Subtype 的 SpEl解析 |
| 3.0.0 | 暂时删除了list实现优化中,增加了xml的方式,增加了性能监控接口 | | |
| 3.0.0 | 暂时删除了list实现优化中,增加了xml的方式,增加了性能监控接口,修复了function 内的 service 需要添加 @Lazy 的问题 | | |

## 使用方式(对象DIFF功能终于支持了)

Expand Down

0 comments on commit 6322a6c

Please sign in to comment.