一、引入依赖jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring4.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
二、spring配置及示例代码
2.1 基于配置文件实现
配置文件
applicationContext.xml
<!-- aop日志配置
<bean id="testLog" class="com.asus.az.rs.log.LogTest"></bean>
<aop:config>
<aop:aspect id="b" ref="testLog">
<aop:pointcut expression="execution(public * com.asus.az.rs.resource..*(..))"
id="log"/>
<aop:before pointcut-ref="log" arg-names="joinpoint" method="before"/>
<aop:after pointcut-ref="log" arg-names="joinpoint" method="after"/>
</aop:aspect>
</aop:config>-->
代码:
//配置文件方式
public class LogTest {
private static final Logger logger = LoggerFactory.getLogger(LogTest.class);
//在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示
//此处的JoinPoint类可以获取,action所有的相关配置信息和request等内置对象。
public void before(JoinPoint joinpoint){
Object[] args = joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
logger.error("方法调用前参数长度 : " + args.length);
String targetName = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
logger.error("方法调用前参数 : " + targetName+";methodName"+methodName);
logger.error("被拦截方法调用之前调用此方法,输出此语句");
}
public void after(JoinPoint joinpoint){
Object[] args = joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
logger.error("方法调用后参数长度 : " + args.length);
String targetName = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
logger.error("方法调用后参数 : " + targetName+";methodName"+methodName);
logger.error("被拦截方法调用之后调用此方法,输出此语句");
}
}
2.2 注解方式
applicationContext.xml
配置文件:
<aop:aspectj-autoproxy/>
代码:
@Component
@Aspect
public class OperateLog {
private static final Logger logger = LoggerFactory.getLogger(OperateLog.class);
private HttpServletRequest request = null;
@Pointcut("execution(* com.asus.az.rs.resource..*(..))")
public void pointCut() {
}
//在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示
//此处的JoinPoint类可以获取,action所有的相关配置信息和request等内置对象。
@Before("pointCut()")
public void before(JoinPoint joinpoint){
// request = getHttpServletRequest();
// Object obj =request.getParameter("account");
// logger.error("方法调用前 account: " + obj);
Object[] args = joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
logger.error("方法调用前参数长度 : " + args.length);
String targetName = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
logger.error("方法调用前参数 : " + targetName+";methodName"+methodName);
logger.error("被拦截方法调用之前调用此方法,输出此语句");
}
@After("pointCut()")
public void after(JoinPoint joinpoint){
Object[] args = joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
logger.error("方法调用后after参数长度 : " + args.length);
String targetName = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
logger.error("方法调用后after参数 : " + targetName+";methodName"+methodName);
logger.error("被拦截方法调用之后调用此方法,输出此语句");
}
@AfterReturning(pointcut="pointCut()",returning="rvt")
public void AfterReturning(JoinPoint joinpoint, Object rvt){
Object[] args = joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
logger.error("方法调用后AfterReturning参数长度 : " + args.length);
logger.error("方法调用后AfterReturning result: " + rvt);
}
@AfterThrowing(throwing="ex",pointcut="pointCut()")
public void afterThrowing(JoinPoint joinpoint, Throwable ex){
Object[] args = joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
logger.error("方法调用后afterThrowing参数长度 : " + args.length);
logger.error("方法调用后afterThrowing ex: " + ex);
}
public HttpServletRequest getHttpServletRequest(){
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
return request;
}
}
三、基于springboot配置AOP
3.1 引入依赖包
<!-- aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
3.2 自定义注解
@Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface LogAnnotation { /** 操作类型. **/ public String operationType() default ""; /** 操作名称. **/ public String operationName() default ""; }
3.3 自定义注解使用
@LogAnnotation(operationType="test",operationName="test111") public ReContent test(final Map<String,String> map)
3.4 代码示例
@Component
@Aspect
public class OperateLogAspect {
private static final Logger logger = LoggerFactory.getLogger(OperateLogAspect.class);
@Pointcut("execution(* com.test.rs.resource..*(..))")
public void pointCut() {
}
//方法调用前执行该方法
@Before("pointCut()")
public void before(JoinPoint joinpoint){
logger.info("方法调用前.");
String methodName = joinpoint.getSignature().getName();
}
/*方法调用后执行该方法*/
@After("pointCut()")
public void after(JoinPoint joinpoint){
logger.info("方法调用后");
}
/*执行方法正常返回*/
@AfterReturning(pointcut="pointCut()",returning="rvt")
public void AfterReturning(JoinPoint joinpoint, Object rvt){
logger.info("方法调用后正常返回");
}
/*执行方法异常返回*/
@AfterThrowing(throwing="ex",pointcut="pointCut()")
public void afterThrowing(JoinPoint joinpoint, Throwable ex){
logger.info("方法调用后异常返回");
}
/*获取自定义注解的值*/
private Map<String,String> getOperationTypeAndName(JoinPoint joinpoint){
Object[] args = joinpoint.getArgs();
String targetName = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
Map<String,String> retMap = new HashMap<String,String>();
try {
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == args.length) {
LogAnnotation la = method.getAnnotation(LogAnnotation.class);
break;
}
}
}
} catch (ClassNotFoundException e) {
logger.error(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}
return retMap;
}
}