java中的aop在项目中的应用

废话不多说,直接上代码吧,具体怎么用看注释

package com.demo.mall.commen.config;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.Date;

/**
 * Mapper切面,插入创建人,创建时间,修改人,修改时间
 * 
 */
@Aspect
@Component
@Configuration
public class MapperAspect {
	private static final String creatorId = "creatorId";
	private static final String creator="creator";
	private static final String createTime = "createTime";
	private static final String updator = "updator";
	private static final String updateTime = "updateTime";

	@Pointcut("execution(* com.demo.mall.*.mapper.*.update*(..))")//扫描mapper层的方法前缀为update***方法
	public void daoUpdate() { //当扫描到执行带有update方法时候,就执行@Around("daoUpdate()")这个注解的方法
	}

	@Pointcut("execution(* com.demo.mall.*.mapper.*.insert*(..))") //扫描mapper层的方法前缀为insert***方法
	public void daoCreate() {
	}

	@Pointcut("execution(* com.demo.mall.*.mapper.*.delete*(..))")//扫描mapper层的方法前缀为delete***方法
	public void daoDelete() {
	}

	@Around("daoUpdate()")
	public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable {
		ServletRequestAttributes attributes = (ServletRequestAttributes) 
		RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return pjp.proceed();
		}
//		HttpServletRequest request = attributes.getRequest();
		Object[] objects = pjp.getArgs();
		if (objects != null && objects.length > 0){
			for (Object arg : objects) {
				BeanUtils.setProperty(arg, updator, "updator");
				BeanUtils.setProperty(arg, updateTime, new Date());
			}
		}
		Object object = pjp.proceed();
		return object;

	}

	@Around("daoCreate()")
	public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable {
		ServletRequestAttributes attributes = (ServletRequestAttributes) 
		RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return pjp.proceed();
		}
		//获取切入点的入参
		Object[] objects = pjp.getArgs();
		if (objects != null && objects.length > 0) {
			for (Object arg : objects) {
				BeanUtils.setProperty(arg, creatorId, "1");
				BeanUtils.setProperty(arg, creator, "user_1");
				BeanUtils.setProperty(arg, updator, "user_1");
				BeanUtils.setProperty(arg, createTime, new Date());
			}
		}
		Object object = pjp.proceed();
		return object;
	}

	@Around("daoDelete()")
	public Object doAroundDelete(ProceedingJoinPoint pjp) throws Throwable {
		ServletRequestAttributes attributes = (ServletRequestAttributes) 
		RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return pjp.proceed();
		}
		//获取切入点的入参
		Object[] objects = pjp.getArgs();
		if (objects != null && objects.length > 0){
			for (Object arg : objects) {
				BeanUtils.setProperty(arg, updator, "delete");
				BeanUtils.setProperty(arg, updateTime, new Date());
			}
		}
		Object object = pjp.proceed();
		return object;
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值