1. 被拦截的方法
//栏目置顶
@PostMapping("/portalDetail_isTop")
@PortalUserLog(descrption = "活动",portalIdPraName = "portalId")
public @ResponseBody ApiResult<?> cirIstopChange(Integer rowId){
try {
portalService.protalIstopChange(rowId);
return ApiResult.success();
} catch (Exception e) {
_logger.error("公告置顶状态变更异常",e);
return ApiResult.fail("置顶状态变更异常");
}
}
2@PortalUserLog()这个注解意思是在这个方法执行前先执行注解的方法。下面是注解的方法 这一段是切面的一个接口
//记录日志自定义注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface PortalUserLog {
//操作名称
String descrption()default "";
String portalIdPraName() default "portalId";
String subDescrption() default "operateName";
}
3 下面是真正的切面要执行的具体方法 本累第一哥方法解释 @Pointcut注解意思是切入的拦截哪个方法,本案例是切入到上面的PortalUserLog接口上 @Pointcut("@annotation(com.wangtiansoft.wisdomedu.cms.config.log.PortalUserLog)") public void sysLogAspect(){…} 这个注解的意思是在sysLogAspect之前执行的内容 @Before(value = “sysLogAspect()”) public void recordLog(JoinPoint joinPoint) throws Throwable {…} @Component
/**
* 记录用户操作portal的内容,万柏林公园路三所学校要求
*/
@Aspect
@Component
@Order(6)
public class PortalLogAspect {
@Value("${customize.portalStyle:}")
private String customizePortalStyleIds;
private ThreadLocal<PortalUserLogEntity> sysLogThreadLocal = new ThreadLocal<>();
/**
* 事件发布是由ApplicationContext对象管控的,
* 需要注入ApplicationContext对象调用publishEvent方法完成事件发布
**/
@Autowired
private ApplicationContext applicationContext;
/***
* 定义controller切入点拦截规则,拦截SysLog注解的方法
*/
@Pointcut("@annotation(com.wangtiansoft.wisdomedu.cms.config.log.PortalUserLog)")
public void sysLogAspect() {
}
/***
* 拦截控制层的操作日志
* @param joinPoint
* @return
* @throws Throwable
*/
@Before(value = "sysLogAspect()")
public void recordLog(JoinPoint joinPoint) throws Throwable {
PortalUserLogEntity sysLog = new PortalUserLogEntity();
sysLogThreadLocal.set(sysLog);
//这里进行判断,如果当前操作的portalId在特殊配置的portalId中,才进行操作
//从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
PortalUserLog annotation = AnnotationUtils.findAnnotation(method, PortalUserLog.class);
//获取定义的参数名称
String portalIdpraName = annotation.portalIdPraName();
String operateName = annotation.subDescrption(); //子描述
//根据切点获取参数的值
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
Integer portalId = new Integer(request.getParameter(portalIdpraName));
request.setAttribute("isSavePortalLog",false);
if (!Strings.isNullOrEmpty(customizePortalStyleIds) && ArrayUtils.contains(customizePortalStyleIds.split(","),portalId+"")){
// 开始时间
long beginTime = Instant.now().toEpochMilli();
User userInfo = SsoUtil.getUserInfo(request);
sysLog.setUserName(userInfo.getAccount());
sysLog.setActionUrl(request.getRequestURI());
sysLog.setStartTime(new Date());
sysLog.setRequestMethod(request.getMethod());
sysLog.setUa(request.getHeader("user-agent"));
//访问目标方法的参数 可动态改变参数值
Object[] args = joinPoint.getArgs();
//获取执行的方法名
sysLog.setActionMethod(joinPoint.getSignature().getName());
// 类名
sysLog.setClassPath(joinPoint.getTarget().getClass().getName());
sysLog.setActionMethod(joinPoint.getSignature().getName());
sysLog.setFinishTime(new Date());
// 参数
sysLog.setParams(Arrays.toString(args));
//操作描述
String subDescription = request.getParameter(operateName);
sysLog.setDescription(annotation.descrption()+(Strings.isNullOrEmpty(subDescription)?"":"-"+subDescription));
long endTime = Instant.now().toEpochMilli();
sysLog.setConsumingTime((int)(endTime - beginTime));
sysLog.setTenantId(TenantUtil.TenantId());
sysLog.setOrgId(portalId);
request.setAttribute("isSavePortalLog",true);
}
}
至此这个切面编程完成在这个置顶功能之前执行了获取这个指定方法的参数有些细节自己还不懂,这方便慢慢的学习。
再写一个例子(自定义注解(写一个切面(相当于拦截器))
定义一个注解类
- @Target(ElementType.METHOD) 一起指定在何处写入注释的合法位置(本案例是在方法上)
- @Retention(RetentionPolicy.RUNTIME) 一起指定注释要保留多长时间(本案例是指在运行是)
- @Documented 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分(本案例指公共的文档化)
- @Inherited 阐述了这个被标注的类型是被继承的。
- 实现注解的类(切面类)
- @Component (把普通pojo实例化到spring容器中,相当于配置文件中的)类不属 于各 种归类的时候(不属于@Controller、@Services等的时候)
- @Aspect 使这个类成为切面
- @Pointcut("@annotation(com.huanshuo 切入点在那个类上面(本案例指的是上面自定义注解的类)
- @Around(“point()”) 环绕增强
使用注解的类
@AddUserToRedis