首页 > 编程知识 正文

注解和aop,spring实现aop的注解

时间:2023-05-05 12:03:24 阅读:112617 作者:4496

目录

1 .基本概念

1.1 .面条类@Aspect

1.2 .触点@Pointcut

1.3 .建议

1.4.JoinPoint

1.5 .运算符用法

1.6.@annotation(annotationtype )。

1.7 .代理对象

1.8 .调用切面评论

2 .代码展示

我在前面写了:在开发过程中,每个方法都需要在执行时进行日志记录、返回值分析,以便于整理AOP相关的知识点。

1 .基本概念1.1 .定义切面类@Aspect切面类,加@Aspect、@Component注释; (以下有展览)

1.2 .触点@Pointcut (1)指定切面方法

execution表达式

第一个*表示匹配任意的方法返回值,

..(两个点)表示零个或多个,

第一个..表示module包及其子包,

第二个*表示所有类,

第三个*表示所有方法,

第二个..表示方法的任意参数个数

//org.je ECG.modules.DLG long.controller; //com.sd3e.project manager.controller.acceptance application; @pointcut(execution )|public * org.je ECG.modules . *.* controller.*.* )|execution * public * com.*.prom @around(excudeservice ) ) ) publicobjectdoaround ) proceedingjoinpointpjp ) throws throws object result=pjp.proced () long time2=system.current time millis (; log.debug ('获取JSON数据需要时间) ) (time2-time1) ) ms ); long start=system.current time millis (; this.parsedicttext(result ); longend=system.current time millis (; log.debug ('将词典注入JSON数据需要时间) (结束-开始) ) ms ); 返回结果; }@pointcut(execution ) public*com.rest.module.*.* () ) ) ) ) 65 )

ic void getMethods() { }

 (2)指定注解 

在这里,自定义了一个注解类,方法加注解即可使用

@Pointcut("@annotation(org.jeecg.common.aspect.annotation.PermissionData)") public void pointCut() { } package org.jeecg.common.aspect.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 数据权限注解 * @Author taoyan * @Date 2019年4月11日 */@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE,ElementType.METHOD})@Documentedpublic @interface PermissionData {/** * 暂时没用 * @return */String value() default "";/** * 配置菜单的组件路径,用于数据权限 */String pageComponent() default "";}

@Pointcut("@annotation(com.rest.utils.SysPlatLog)") public void withAnnotationMethods() { } public @interface SysPlatLog { // 操作名称 String operateName() default ""; // 操作描述 String logNote() default "";} 1.3.Advice

在切入点上执行的增强处理,主要有五个注解: 

@Before 在切点方法之前执行@After 在切点方法之后执行@AfterReturning 切点方法返回后执行@AfterThrowing 切点方法抛异常执行@Around 属于环绕增强,能控制切点执行前,执行后 1.4.JoinPoint

方法中的参数JoinPoint为连接点对象,它可以获取当前切入的方法的参数、代理类等信息,因此可以记录一些信息,验证一些信息等;

1.5.运算符用法

使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系; 

   1.2.代码中给出了示例

1.6.@annotation(annotationType)

匹配指定注解为切入点的方法;

1.7.代理对象  //aop代理对象Object aThis = joinPoint.getThis();//被代理对象Object target = joinPoint.getTarget(); 1.8.调用切面注解 @AutoLog(value = "ws_xbx-分页列表查询")@ApiOperation(value="ws_xbx-分页列表查询", notes="ws_xbx-分页列表查询")@GetMapping(value = "/list")@PermissionData(pageComponent="sd3e/actDemo/WsXbxList")//sys_permission 表组件路径 component 字段public Result<?> queryPageList(WsXbx wsXbx, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {QueryWrapper<WsXbx> queryWrapper = QueryGenerator.initQueryWrapper(wsXbx, req.getParameterMap());Page<WsXbx> page = new Page<WsXbx>(pageNo, pageSize);IPage<WsXbx> pageList = wsXbxService.page(page, queryWrapper);return Result.OK(pageList);}

@SysPlatLog(operateName = "查看详情",logNote = "查看详情")@SneakyThrowspublic Result getItem(@RequestParam(required = false) String bs){}  2.代码展示 package com.npc.rest.utils;import com.npc.rest.common.properites.AppSupPlatProperties;import com.sys.model.SysLog;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;import java.lang.reflect.Method;import java.util.*;@Aspect@Slf4j@Componentpublic class SysPlatLogAspect { /** * 指定切面 */ @Pointcut("execution(public * com.rest.module..*.*(..))") public void getMethods() {   } /** * 指定注解 */ @Pointcut("@annotation(com.rest.utils.SysPlatLog)") public void withAnnotationMethods() { } /*** * 拦截控制层的操作日志 * @param joinPoint * @return * @throws Throwable */ @After(value = "getMethods() && withAnnotationMethods()") public void recordLog(JoinPoint joinPoint) throws Throwable { SysLog sysLog = new SysLog(); SysPlatLog sysPlatLog = getInter(joinPoint); sysLog.setOperateName(sysPlatLog.operateName()); sysLog.setLogNote(sysPlatLog.logNote()); sysLog.setLogTime(new Date()); sysLog.setAppCode(AppSupPlatProperties.getAppCode()); } public SysPlatLog getInter(JoinPoint joinPoint) throws ClassNotFoundException { String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { SysPlatLog sysPlatLog = method.getAnnotation(SysPlatLog.class); return sysPlatLog; } } } return null; }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。