转载

【知识杂谈】spring自定义注解剖析(一)

前言

spring提供了丰富的注解,但有时并不能满足现有业务复杂的需求,我们可以通过自定义注解完善我们的业务框架。

寄语:

孰能生巧,天道酬勤,是走向成功的必经之路。

注解创建说明

@Target

@Target –注解用于什么地方,默认值为任何元素,表示该注解用于什么地方。可用的ElementType指定参数

● ElementType.CONSTRUCTOR:用于描述构造器

● ElementType.FIELD:成员变量、对象、属性(包括enum实例)

● ElementType.LOCAL_VARIABLE:用于描述局部变量

● ElementType.METHOD:用于描述方法

● ElementType.PACKAGE:用于描述包

● ElementType.PARAMETER:用于描述参数

● ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明

@Retention

表示需要在什么级别保存该注解信息。

什么时候使用该注解,即注解的生命周期,使用RetentionPolicy来指定

● RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们 不会写入字节码。@Override, @SuppressWarnings都属于这类注解。

● RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式

● RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

@Document

将注解包含在Javadoc中

@Inherited

允许子类继承父类中的注解

实例说明

OperationType.java

`package com.basic.bl.rest.demo.aop;

public enum OperationType {

/**
 * 操作类型
 */
UNKNOWN("unknown"),
DELETE("delete"),
SELECT("select"),
UPDATE("update"),
INSERT("insert");

private String value;

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

OperationType(String s) {
    this.value = s;
}

}`

OperationUnit.java

`package com.basic.bl.rest.demo.aop;

public enum OperationUnit {

/**
 * 被操作的单元
 */
UNKNOWN("unknown"),
USER("user"),
EMPLOYEE("employee"),
SELLER("seller");

private String value;

OperationUnit(String value) {
    this.value = value;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

}

`

OperationLog.java

`package com.basic.bl.rest.demo.aop;

import java.util.Date;

public class OperationLog {

private String id;
private Date createTime;
/**
 * 日志等级
 */
private Integer level;
/**
 * 被操作的对象
 */
private String operationUnit;
/**
 * 方法名
 */
private String method;
/**
 * 参数
 */
private String args;
/**
 * 操作人id
 */
private String userId;
/**
 * 操作人
 */
private String userName;
/**
 * 日志描述
 */
private String describe;
/**
 * 操作类型
 */
private String operationType;
/**
 * 方法运行时间
 */
private Long runTime;
/**
 * 方法返回值
 */
private String returnValue;

@Override
public String toString() {
    return "OperationLog{" +
            "id='" + id + '/'' +
            ", createTime=" + createTime +
            ", level=" + level +
            ", operationUnit='" + operationUnit + '/'' +
            ", method='" + method + '/'' +
            ", args='" + args + '/'' +
            ", userId='" + userId + '/'' +
            ", userName='" + userName + '/'' +
            ", describe='" + describe + '/'' +
            ", operationType='" + operationType + '/'' +
            ", runTime=" + runTime +
            ", returnValue='" + returnValue + '/'' +
            '}';
}

public Long getRunTime() {
    return runTime;
}

public void setRunTime(Long runTime) {
    this.runTime = runTime;
}

public String getReturnValue() {
    return returnValue;
}

public void setReturnValue(String returnValue) {
    this.returnValue = returnValue;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Date getCreateTime() {
    return createTime;
}

public void setCreateTime(Date createTime) {
    this.createTime = createTime;
}

public Integer getLevel() {
    return level;
}

public void setLevel(Integer level) {
    this.level = level;
}

public String getOperationUnit() {
    return operationUnit;
}

public void setOperationUnit(String operationUnit) {
    this.operationUnit = operationUnit;
}

public String getMethod() {
    return method;
}

public void setMethod(String method) {
    this.method = method;
}

public String getArgs() {
    return args;
}

public void setArgs(String args) {
    this.args = args;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getDescribe() {
    return describe;
}

public void setDescribe(String describe) {
    this.describe = describe;
}

public String getOperationType() {
    return operationType;
}

public void setOperationType(String operationType) {
    this.operationType = operationType;
}

}

`

OperationLogDetail.java

`package com.basic.bl.rest.demo.aop;

import java.lang.annotation.*;

//@OperationLogDetail(detail = "通过手机号[{{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)

@Documented

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface OperationLogDetail {

/**
 * 方法描述,可使用占位符获取参数:{{tel}}
 */
String detail() default "";

/**
 * 日志等级:自己定,此处分为1-9
 */
int level() default 0;

/**
 * 操作类型(enum):主要是select,insert,update,delete
 */
OperationType operationType() default OperationType.UNKNOWN;

/**
 * 被操作的对象(此处使用enum):可以是任何对象,如表名(user),或者是工具(redis)
 */
OperationUnit operationUnit() default OperationUnit.UNKNOWN;

}

`

LogAspect.java

`package com.basic.bl.rest.demo.aop;

import com.alibaba.fastjson.JSON;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.stereotype.Component;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.UUID;

@Aspect

@Component

public class LogAspect {

/**
 * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果
 * '@Pointcut("execution(* com.basic.bl.rest.demo.aop.service.impl.*.*(..))")'
 */
@Pointcut("@annotation(com.basic.bl.rest.demo.aop.OperationLogDetail)")
public void operationLog(){}


/**
 * 环绕增强,相当于MethodInterceptor
 */
@Around("operationLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object res = null;
    long time = System.currentTimeMillis();
    try {
        res =  joinPoint.proceed();
        time = System.currentTimeMillis() - time;
        return res;
    } finally {
        try {
            //方法执行完成后增加日志
            addOperationLog(joinPoint,res,time);
        }catch (Exception e){
            System.out.println("LogAspect 操作失败:" + e.getMessage());
            e.printStackTrace();
        }
    }
}

private void addOperationLog(JoinPoint joinPoint, Object res, long time){
    MethodSignature signature = (MethodSignature)joinPoint.getSignature();
    OperationLog operationLog = new OperationLog();
    operationLog.setRunTime(time);
    operationLog.setReturnValue(JSON.toJSONString(res));
    operationLog.setId(UUID.randomUUID().toString());
    operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs()));
    operationLog.setCreateTime(new Date());
    operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName());
    operationLog.setUserId("#{currentUserId}");
    operationLog.setUserName("#{currentUserName}");
    OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
    if(annotation != null){
        operationLog.setLevel(annotation.level());
        operationLog.setDescribe(getDetail(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation));
        operationLog.setOperationType(annotation.operationType().getValue());
        operationLog.setOperationUnit(annotation.operationUnit().getValue());
    }
    //TODO 这里保存日志
    System.out.println("记录日志:" + operationLog.toString());

// operationLogService.insert(operationLog);

}

/**
 * 对当前登录用户和占位符处理
 * @param argNames 方法参数名称数组
 * @param args 方法参数数组
 * @param annotation 注解信息
 * @return 返回处理后的描述
 */
private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation){

    Map<Object, Object> map = new HashMap<>(4);
    for(int i = 0;i < argNames.length;i++){
        map.put(argNames[i],args[i]);
    }

    String detail = annotation.detail();
    try {
        detail = "'" + "#{currentUserName}" + "'=》" + annotation.detail();
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            Object k = entry.getKey();
            Object v = entry.getValue();
            detail = detail.replace("{{" + k + "}}", JSON.toJSONString(v));
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return detail;
}

@Before("operationLog()")
public void doBeforeAdvice(JoinPoint joinPoint){
    System.out.println("进入方法前执行.....");

}

/**
 * 处理完请求,返回内容
 * @param ret
 */
@AfterReturning(returning = "ret", pointcut = "operationLog()")
public void doAfterReturning(Object ret) {
    System.out.println("方法的返回值 : " + ret);
}

/**
 * 后置异常通知
 */
@AfterThrowing("operationLog()")
public void throwss(JoinPoint jp){
    System.out.println("方法异常时执行.....");
}


/**
 * 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
 */
@After("operationLog()")
public void after(JoinPoint jp){
    System.out.println("方法最后执行.....");
}

}`

DemoController.java

`package com.basic.bl.rest.demo.aop;

import com.basic.bl.rest.demo.aop.service.DemoService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping("demo")

public class DemoController {

@Autowired
private DemoService demoService;

/**
 * 访问路径 http://localhost:11000/user/findUserNameByTel?tel=1234567
 * @param tel 手机号
 * @return userName
 */
@ResponseBody
@RequestMapping("/findUserNameByTel")
public String findUserNameByTel(@RequestParam("tel") String tel){
    return demoService.findUserName(tel);
}

}

``

DemoServiceImpl.java

`package com.basic.bl.rest.demo.aop.service.impl;

import com.basic.bl.rest.demo.aop.OperationLogDetail;

import com.basic.bl.rest.demo.aop.OperationType;

import com.basic.bl.rest.demo.aop.OperationUnit;

import com.basic.bl.rest.demo.aop.service.DemoService;

import org.springframework.stereotype.Service;

@Service

public class DemoServiceImpl implements DemoService {

@OperationLogDetail(detail = "通过手机号[{{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
@Override
public String findUserName(String tel) {
    System.out.println("tel:" + tel);
    return "zhangsan";
}

}`

DemoService.java

`package com.basic.bl.rest.demo.aop.service;

public interface DemoService {

/**
 * 获取用户信息
 * @return
 * @param tel
 */
String findUserName(String tel);

}

`

结果

【知识杂谈】spring自定义注解剖析(一)

【知识杂谈】spring自定义注解剖析(一)

进入方法前执行.....

tel:1721212121

记录日志:OperationLog{id='b8dc5682-0c68-4efb-9841-8a929dc91663', createTime=Tue Dec 03 00:45:26 CST 2019, level=3, operationUnit='user', method='com.basic.bl.rest.demo.aop.service.impl.DemoServiceImpl.findUserName', args='["1721212121"]', userId='#{currentUserId}', userName='#{currentUserName}', describe=''#{currentUserName}'=》通过手机号["1721212121"]获取用户名', operationType='select', runTime=5, returnValue='"zhangsan"'}

方法最后执行.....

方法的返回值 : zhangsan

原文  https://segmentfault.com/a/1190000021176720
正文到此结束
Loading...