spring AOP切面编程——基于自定义注解

AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,

    (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知

	(2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用

	(3)Advice(通知):AOP在特定的切入点上执行的增强处理,有before,after,afterReturning,afterThrowing,around

	(4)Pointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式

	(5)AOP代理:AOP框架创建的对象,代理就是目标对象的加强。Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类

需要jar包 spring-aspects-3.2.0.RELEASE.jar、spring-aop-3.2.0.RELEASE.jar

//切入控制层
@SysLog("修改密码")


自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

	String value() default "";
}

切面编程的实际使用

@Aspect
@Component
public class SysLogAspect {
	@Autowired
	private SysLogService sysLogService;
	
1、执行到这
	@Pointcut("@annotation(io.renren.common.annotation.SysLog)")
	public void logPointCut() { 
		
	}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		//执行方法
		
2、执行到contorl
		Object result = point.proceed();
		//执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;

3、执行完contorl后执行此处
		//保存日志
		saveSysLog(point, time);

		return result;
	}

	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		
4、ProceedingJoinPoint joinPoint \\MethodSignature signature \\Method method \\joinPoint.getTarget().getClass().getName()
		SysLogEntity sysLog = new SysLogEntity();
		SysLog syslog = method.getAnnotation(SysLog.class);
		if(syslog != null){
			//注解上的描述
			sysLog.setOperation(syslog.value());
		}

		//请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLog.setMethod(className + "." + methodName + "()");

		//请求的参数
		Object[] args = joinPoint.getArgs();
		try{
			String params = new Gson().toJson(args[0]);
			sysLog.setParams(params);
		}catch (Exception e){

		}

		//获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		//设置IP地址
		sysLog.setIp(IPUtils.getIpAddr(request));

		//用户名
		String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
		sysLog.setUsername(username);

		sysLog.setTime(time);
		sysLog.setCreateDate(new Date());
		//保存系统日志
		sysLogService.insert(sysLog);
	}
}

底层源码__动态代理调用、

 public Object proceed() throws Throwable {
    		return this.methodInvocation.invocableClone().proceed();
    	}

spring AOP底层原理 详见 https://blog.csdn.net/eson_15/article/details/84933442

猜你喜欢

转载自blog.csdn.net/e_shi_yi_p/article/details/85046954