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();
}
}
本文详细介绍如何在JAVA中创建并使用自定义注解。通过定义注解接口、使用元注解,以及实现AOP切面来拦截和处理带有自定义注解的方法,展示了一种增强代码功能的有效方式。
669

被折叠的 条评论
为什么被折叠?



