目录
AOP的使用场景:
权限认证、日志、事务处理、增强处理
一、自定义注解日志具体代码实现
1.1 定义注解
1.1.1 @Target:注解的作用目标
@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包
1.1.2 @Retention:注解的保留位置
RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。
RetentionPolicy.CLASS:这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。
RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。
1.1.3 @Document:说明该注解将被包含在javadoc中
1.1.4 @Inherited:说明子类可以继承父类中的该注解
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAnnotation {
String value();
}
1.2 切面定义
1.2.1 Advice,在切入点上执行的增强处理,主要有五个注解
@Before 在切点方法之前执行
@After 在切点方法之后执行
@AfterReturning 切点方法返回后执行
@AfterThrowing 切点方法抛异常执行
@Around 属于环绕增强,能控制切点执行前,执行后
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import com.cn.log.LogAnnotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Component
@Aspect //定义切面类,加上@Aspect、@Component注解
public class LogInterceptor {
/**
* 日志dao
*/
@Autowired
private LogService logService;
/**
* 记录操作日志
*
* @param point
* @throws Exception
*/
//execution表达式第一个*表示匹配任意的方法返回值,第二个*表示所有service包下的类,第三个*表示所有方法,第一个..表示任意参数个数。@annotation:用于匹配当前执行方法持有指定注解的方法
@AfterReturning(value = "execution(public * com.cn.service.*.*(..)) && @annotation(com.cn.log.LogAnnotation)")
public void record(JoinPoint point) throws Exception {//JoinPoint :方法中的参数JoinPoint为连接点对象,它可以获取当前切入的方法的参数、代理类等信息,因此可以记录一些信息,验证一些信息等;
// 获得注释内容
LogAnnotation log = getAnnotation(point);
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
if (null == request)
return;
SysUser user; // 补充获取当前用户信息的方法
if (null != user){
//添加日志
logService.insertLog(user.getUserName(), user.getSessionId(),user.getNickName(),
log.value(),point.getSignature().toString() //被修饰的方法名
,com.alibaba.fastjson.JSONArray.toJSON(point.getArgs()).toString());//被注解所修饰的方法参数信息
}
}
/**
* 获得注释内容
*
* @param joinPoint
* @return
*/
private static LogAnnotation getAnnotation(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(LogAnnotation.class);
}
return null;
}
}
1.3 注解切面的使用
在该方法执行完毕之后就会进行日志的添加
/**修改权限人员
* @param id id
* @param powerUsers 编码list
*/
@LogAnnotation("用户导入模板授权")
public void addPowerUser(int id, String powerUsers) {
configMapper.addPowerUser(id,powerUsers);
}