开启自动注入配置注解
package com.yh.watercloud.dynamic;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 开启自动注入配置
*
* @Description
* @Author wzq
* @Date 2023/4/19 11:48
**/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(AutoInjectValueConfigurationRegistrar.class)
public @interface EnableAutoInjectValue {
boolean enable() default true;
}
import bean 注册配置
package com.yh.watercloud.dynamic;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
/**
* AutoInjectValueConfigurationRegistrar
* import bean 注册配置
*
* @Description
* @Author wzq
* @Date 2023/4/19 13:05
**/
public class AutoInjectValueConfigurationRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableAutoInjectValue.class.getName()));
boolean enable = attributes.getBoolean("enable");
if (enable) {
RootBeanDefinition root = new RootBeanDefinition(AutoInjectValueAspect.class);
registry.registerBeanDefinition("autoInjectValueAspect", root);
}
}
}
注入aop切面
package com.yh.watercloud.dynamic;
import cn.hutool.core.util.ObjectUtil;
import com.yh.watercloud.common.constant.CommonConstants;
import com.yh.watercloud.common.context.UserDtoContextHolder;
import com.yh.watercloud.common.domain.BaseDO;
import com.yh.watercloud.common.exception.GlobalException;
import com.yh.watercloud.common.utils.CodeMsg;
import com.yh.watercloud.common.utils.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
/**
* 自动注入sql实体值:创建人等
*
* @Description
* @Author wzq
* @Date 2023/4/19 10:49
**/
@Slf4j
@Aspect
public class AutoInjectValueAspect {
@Pointcut("( execution(* com.yh.watercloud..*.dao..*.insert(..)) ||" +
" execution(* com.yh.watercloud..*.dao..*.updateById(..)) ||" +
" execution(* com.yh.watercloud..*.dao..*.update(..)) )")
public void pointCut() {
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) throws Throwable {
log.info("进入了自动赋值sql实体的切面");
//方法名
String methodName = joinPoint.getSignature().getName();
//获取参数
Object[] args = joinPoint.getArgs();
Object param1 = args[0];
if (param1 instanceof BaseDO) {
BaseDO baseDO = (BaseDO) param1;
UserDto userDto = UserDtoContextHolder.get();
if (methodName.contains("insert")) {
if (userDto != null) {
baseDO.setCreatorId(userDto.getId());
baseDO.setCreateBy(userDto.getName());
baseDO.setUpdaterId(userDto.getId());
baseDO.setUpdatedBy(userDto.getName());
}
} else if (methodName.contains("update")) {
baseDO.setUpdaterId(userDto.getId());
baseDO.setUpdatedBy(userDto.getName());
}
}
}
}
用户信息上下文线程变量
package com.yh.watercloud.common.context;
import com.yh.watercloud.common.utils.UserDto;
/**
* 用户信息上下文
*
* @Description
* @Author wzq
* @Date 2023/4/19 13:52
**/
public class UserDtoContextHolder {
/**
* 登陆用户信息
*/
private static final ThreadLocal<UserDto> contextHolder = new ThreadLocal<UserDto>() {
@Override
protected UserDto initialValue() {
return new UserDto();
}
};
public static void set(UserDto userDto) {
contextHolder.set(userDto);
}
public static UserDto get() {
return contextHolder.get();
}
public static void clear() {
contextHolder.remove();
}
}
在过滤器或者Controller的切面中赋值,返回时清空
before:
//放入本地线程变量中
UserDtoContextHolder.set(user);
AfterReturning:
//清空本地线程变量
UserDtoContextHolder.clear();