preface

When we do the development of Springboot project will encounter a variety of annotations, using a variety of annotations, greatly simplified our development process, way, from JDK5 to support annotations is a powerful feature of the Java language

It can be understood as special tags on code that can be read and processed during the life cycle of a program class, such as compilation, class loading, and execution. Annotations allow developers to embed supplementary information in source code without changing the original code and logic

Custom annotations

methods

package cn.soboys.kmall.common.annotation;

import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.PACKAGE, ElementType.PARAMETER ,ElementType.TYPE,ElementType.FIELD})
@Documented
@Inherited
public @interface SysLog {

    String value(a) default "";
    String message(a);
    String[] names() default {};
}
Copy the code

@inherited this custom annotation is an annotation that can be Inherited. So String value() defines the annotation’s member variable by method value default key by default. String message(); String[] names(); Annotation member variables message and names are defined by methods, where names is a String array

1. Basic Types: Byte, short, char, int, long, float, double 2.String 3.Class 4. Enum 5.Annotation It then determines whether the class or method meets the criteria based on the value of the field in the annotation and then deals with its own business logic

Real column usage scenarios

It is generally used in conjunction with Aop and is used through background logging as a usage scenario. Please refer to my previous articles for details on Aop usage

1. Define user-defined SysLog annotations

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

	String value(a) default "";
}
Copy the code

2. Define the log interception aspect SysLogAspect, which resolves controllers with SysLog annotations

@Around("@annotation(sysLog)")
	public Object around(ProceedingJoinPoint joinPoint,com.yami.shop.common.annotation.SysLog sysLog) throws Throwable {
		long beginTime = SystemClock.now();
		// Execute method
		Object result = joinPoint.proceed();
		// Execution time (ms)
		long time = SystemClock.now() - beginTime;


		SysLog sysLogEntity = new SysLog();
		if(sysLog ! = null){// The description in the annotation
			sysLogEntity.setOperation(sysLog.value());
		}

		// The method name of the request
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		sysLogEntity.setMethod(className + "." + methodName + "()");

		// Request parameters
		Object[] args = joinPoint.getArgs();
		String params = Json.toJsonString(args[0]);
		sysLogEntity.setParams(params);

		// Set the IP address
		sysLogEntity.setIp(IPHelper.getIpAddr());

		/ / user name
		String username = SecurityUtils.getSysUser().getUsername();
		sysLogEntity.setUsername(username);

		sysLogEntity.setTime(time);
		sysLogEntity.setCreateDate(new Date());
		// Save system logs
		sysLogService.save(sysLogEntity);


		return result;
	}
Copy the code

The loose coupling of annotations is a great programming benefit.