码迷,mamicode.com
首页 > 其他好文 > 详细

AOP统一处理修改人、创建人、修改时间、创建时间

时间:2016-10-10 19:53:15      阅读:951      评论:0      收藏:0      [点我收藏+]

标签:

1.配置拦截

 

<bean id="dalAop" class="com.biz.common.aop.DalAop" />
<aop:config>
<aop:aspect ref="dalAop" > 
<aop:before method="beforeInsert" pointcut="execution(* com.dal.*.mapper.*.insert*(..))" />
<aop:before method="beforeUpdate" pointcut="execution(* com.dal.*.mapper.*.update*(..))" />
<aop:before method="beforeUpdate" pointcut="execution(* com.dal.*.mapper.*.remove*(..))" />
<aop:before method="beforeUpdate" pointcut="execution(* com..dal.*.mapper.*.delete*(..))" />
</aop:aspect>
</aop:config>

2.实体类

package com.biz.common.aop;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import com.auth.privilege.PrivilegeInfo;

/**
 * 类DalAop.java的实现描述:对DAO插入和更新方法的拦截,并设置公共字段的信息
 * 
 * 
 */
public class DalAop {

    private static final String CREATOR     = "creator";
    private static final String MODIFIER    = "modifier";
    private static final String GMTCREATE   = "gmtCreate";
    private static final String GMTMODIFIED = "gmtModified";
    @Resource
    private PrivilegeInfo       privilegeInfo;

    public void beforeInsert(JoinPoint jp) {
        Object[] args = jp.getArgs();

        if (args != null && args.length > 0) {
            Object argument = args[0];
            BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
            // 设置创建时间和修改时间
            if (beanWrapper.isWritableProperty(GMTCREATE)) {
                beanWrapper.setPropertyValue(GMTCREATE, new Date());
            }
            if (beanWrapper.isWritableProperty(GMTMODIFIED)) {
                beanWrapper.setPropertyValue(GMTMODIFIED, new Date());
            }
            // 设置创建人和修改人
            if (beanWrapper.isWritableProperty(CREATOR) && privilegeInfo != null
                && StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
                beanWrapper.setPropertyValue(CREATOR, privilegeInfo.getWorkNo());
            }
            if (beanWrapper.isWritableProperty(MODIFIER) && privilegeInfo != null
                && StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
                beanWrapper.setPropertyValue(MODIFIER, privilegeInfo.getWorkNo());
            }
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void beforeUpdate(JoinPoint jp) {

        Object[] args = jp.getArgs();
        // myBatis只能传递一个参数
        if (args != null && args.length > 0) {
            Object argument = args[0];

            // 如果argument是map类型
            if (argument instanceof Map) {
                Map map = (Map) argument;
                map.put(GMTMODIFIED, new Date());
                if (privilegeInfo != null && StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
                    map.put(MODIFIER, privilegeInfo.getWorkNo());
                }
            } else {
                BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
                if (beanWrapper.isWritableProperty(GMTMODIFIED)) {
                    beanWrapper.setPropertyValue(GMTMODIFIED, new Date());
                }
                if (beanWrapper.isWritableProperty(MODIFIER) && privilegeInfo != null
                    && StringUtils.isNotBlank(privilegeInfo.getWorkNo())) {
                    beanWrapper.setPropertyValue(MODIFIER, privilegeInfo.getWorkNo());
                }
            }
        }
    }
}

 

AOP统一处理修改人、创建人、修改时间、创建时间

标签:

原文地址:http://www.cnblogs.com/nunuAction/p/5946627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!