第一步,在配置里面进行配置:
<?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;
}