JAVA自定义注解
1.定义一个接口@interface
这几个注解一定要加上
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Timing {
}
2.写自定义注解的实现方法
@Aspect
@Component
public class TimingAspect {
@Autowired
protected RedisService redisService;
private static final Logger logger = LoggerFactory.getLogger(TimingAspect.class);
//切入点
@Pointcut(value = "@annotation(com.lydsoft.eip.mapp.common.annotation.Timing)")
private void pointcut() {
}
@Around(value = "pointcut()")
public void around(ProceedingJoinPoint point) throws Throwable {
//拦截的类名
String className = point.getTarget().getClass().getSimpleName();
className = className.substring(0, className.indexOf("$"));
//拦截的方法
String methodName = ((MethodSignature) point.getSignature()).getMethod().getName();
logger.info(className + "." + methodName);
// 执行方法
point.proceed();
}
}