在实际开发中,很多时刻我们会有记录请求日志,定时任务日志等需求,在每个方法中都编写相同的代码去记录日志显然是不合理的。
Spring已经为我们提供了面向切面编程的思想,不妨简单的使用下自定义注解。
简单自定义注解分四步:
1:在配置中打开aop编程
<!-- 自定义AOP -->
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="serviceAspect" />
</aop:aspectj-autoproxy>
<bean id = "serviceAspect" class="com.thc.tenantcenter.aspect.LogAspect"></bean>
class指向的是切面解析类下文会有提到。(在spring-boot中,我们可以直接使用aop而不需要任何的配置)
2:编写自己自定义的注解
package com.thc.tenantcenter.aspect;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LogAnnotation {
String desc() default "打印日志";
}
3:编写自己的切面实现类,就是上文所说的bean指向的路径
package com.thc.tenantcenter.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect //该注解标示该类为切面类
@Component //注入依赖
public class LogAspect {
//标注该方法体为后置通知,当目标方法执行成功后执行该方法体
@AfterReturning("within(com.thc.tenantcenter..*) && @annotation(rl)")
public void addLogSuccess(JoinPoint jp, LogAnnotation rl){
Object[] parames = jp.getArgs();//获取目标方法体参数
for (int i = 0; i < parames.length; i++) {
System.out.println(parames[i]);
}
System.out.println(jp.getSignature().getName());
String className = jp.getTarget().getClass().toString();//获取目标类名
System.out.println("className:" + className);
className = className.substring(className.indexOf("com"));
String signature = jp.getSignature().toString();//获取目标方法签名
System.out.println("signature:" + signature);
}
}
4:在com.thc.tenantcenter包下及子包下方法上添加自己定义的注解
@LogAnnotation(desc = "通过ID获取实体信息")
@Override
public AdminLog getAdminLogById(Integer id) {
return adminLogMapper.getAdminLogById(id);
}
运行结果为:
1
getAdminLogById
className:class com.thc.tenantcenter.biz.adminlog.service.impl.AdminLogServiceImpl
signature:AdminLog com.thc.tenantcenter.biz.adminlog.service.impl.AdminLogServiceImpl.getAdminLogById(Integer)
四种元注解
JDK5.0版本在 java.lang.annotation提供了四种元注解,专门注解其他的注解:
@Documented – 是否将注解信息添加在java文档中
@Retention – 定义该注解的生命周期
@Target –注解用于什么地方
@Inherited – 是否允许子类继承该注解
@Retention 注解定义该注解的生命周期,在什么阶段丢弃:
RetentionPolicy.SOURCE – 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
RetentionPolicy.CLASS – 在类加载的时候丢弃,在字节码文件的处理中有用。注解默认使用这种方式。
RetentionPolicy.RUNTIME – 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。自定义的注解通常使用这种方式。
@Target – 表示该注解用于什么地方。如果不明确指出,该注解可以放在任何地方。属性的注解是兼容的。
ElementType.TYPE:用于描述类、接口或enum声明
ElementType.FIELD:用于描述实例变量
ElementType.METHOD
ElementType.PARAMETER
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.ANNOTATION_TYPE 另一个注释
ElementType.PACKAGE 用于记录java文件的package信息
那么,注解的内部到底是如何定义的呢?
Annotations只支持基本类型、String及枚举类型。注释中所有的属性被定义成方法,并允许提供默认值。
原文地址:
https://www.cnblogs.com/zhuxiansheng/p/7805552.html
https://juejin.im/post/5d1b2312f265da1bc23f913e?utm_source=gold_browser_extension