采用@Aspect进行拦截操作

22 篇文章 0 订阅
6 篇文章 0 订阅

第一步,在配置里面进行配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
           http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/jdbc    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
       "
	default-autowire="byName">

	
	<!-- 记录任务日志注解 begin -->
	<bean id="testLogAspect" class="com.test.TestLogAspect"></bean>
	<aop:config>
		<aop:aspect ref="testLogAspect">
			<aop:around pointcut="@annotation(s)" method="handleRequest"
				arg-names="s" />
		</aop:aspect>
	</aop:config>
	<!-- 记录任务日志注解 end -->

</beans>

第二部编写对应实体类

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;


/**
 * 类InstanceLogAspect.java的实现描述:<br/>
 * 保存任务实例执行日志
 * 
 * @author zhoujie 2016年8月5日 下午3:26:55
 */
@Aspect
@Slf4j
public class TestLogAspect {

    @SuppressWarnings("rawtypes")
    @Around("within(com.test..*) && @annotation(testLogSaver)")
    public Object handleRequest(ProceedingJoinPoint pj, TestLogSaver testLogSaver) throws Throwable {
        // 入参
        Object[] inputArgs = pj.getArgs();
        String testId = getValue(inputArgs,testLogSaver.testId());
        String source = getValue(inputArgs,testLogSaver.source());
        // 业务处理
        ...

        // 返回值
        Object returnObj = null;
        // 业务处理
        try {
            returnObj = pj.proceed();
        } catch (Exception e) {
            log.error("InstanceLogAspect --> handleRequest,执行异常。", e);
            throw e;
        }

        // 业务处理结果
       

        return returnObj;
    }


    /**
     * 获取拦截方法参数值
     * 
     * @param inputArgs
     * @param key
     * @return
     */
    private String getValue(Object[] inputArgs, String key) {
        String value = null;
        PropertyUtilsBean bean = new PropertyUtilsBean();
        try {
            if (null != inputArgs[0]) {
                Object refObj;
                if (inputArgs[0] instanceof Object[]) {
                    refObj = ((Object[]) (inputArgs[0]))[0];
                } else if (inputArgs[0] instanceof Collection) {
                    refObj = ((Collection<?>) (inputArgs[0])).toArray()[0];
                } else {
                    refObj = inputArgs[0];
                }

                if (null != refObj && StringUtils.isNotBlank(key)) {
                    // 获取值
                    value = bean.getProperty(refObj, key) == null ? "" : bean.getProperty(refObj, key).toString();
                }
            }
        } catch (Exception e) {
            log.error("通过属性名称:{},获取值异常。", key, e);
        }

        return value;
    }

}


 
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface TestLogSaver {

    /**
     * 任务id
     * 
     * @return
     */
    String testId();

    /**
     * 执行源头
     */
    String source();

}

第三部,在对应需要拦截的方法上加上该注解

@Override
    @TestLogSaver(testId = "testDTO.id", source = "source")
    public ResultBase execJob(InputParamsDTO paramsDTO) {
        业务操作。。。。。。。。。
    }

public class InputParamsDTO  {

    /**
     * serialVersionUID
     */
    private static final long        serialVersionUID = -3365052574533394124L;
  

    /**
     * 执行实例
     */
    private TestDTO testDTO;

    /**
     * 执行来源
     */
    private String                   source;

}
public class TestDTO  {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 2138499631988733990L;
  
    private Long              id;

   
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值