注解类
package com.atlp.project.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Author: zhangchq
* @CreateTime: 2019年08月09日 14时24分
* @Decription:
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AtlpLogAspect {
}
注解方式
around增强处理。虽然Around功能强大,但通常需要在线程安全的环境下使用。因此,如果使用普通的Before、AfterReturing增强方法就可以解决的事情,就没有必要使用Around增强处理了。
package com.atlp.project.aop;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Parameter;
/**
* @Author: zhangchq
* @CreateTime: 2019年08月09日 13时16分
* @Decription:
*/
@Slf4j
@Aspect
@Component
public class AitlampLogAspect {
// 定义增强,pointcut连接点使用@annotation(xxx)进行定义
@Around("@annotation(com.atlp.project.aop.AtlpLogAspect)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 目标类的名称
String className = joinPoint.getTarget().getClass().getName();
// 方法名称
String methodName = joinPoint.getSignature().getName();
// 参数列表
Parameter[] parameters = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameters();
Object[] args = joinPoint.getArgs();
StringBuilder params = new StringBuilder("[");
for (int i = 0; i < parameters.length; i++) {
params.append(parameters[i].getName()).append(":").append(args[i]).append("; ");
}
if (parameters.length > 0) {
int length = params.length();
params.delete(length - 2, length);
}
params.append("]");
log.info(">>>> Target class name={} | method name={} | params={} <<<<",className,methodName,params);
Object result = null;
try {
// 执行方法
result = joinPoint.proceed();
} catch (Exception e) {
// 记录异常日志
log.error(">>>> Target class name={} | method name={}\n{}\n\t at {} <<<<",className, methodName, e.toString(), e.getStackTrace()[0]);
throw e;
}
// 记录方法结果
log.info(">>>> Target class name={} | method name={} | results={} <<<<",className, methodName, JSON.toJSON(result));
return result;
}
}
Controller
package com.atlp.project.controller.login;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atlp.project.aop.AtlpLogAspect;
import com.atlp.project.pojo.LoginPojo;
import com.atlp.project.service.login.IAtlpLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.atlp.base.BaseController;
import org.atlp.base.UserInfo;
import org.atlp.exception.BusinessException;
import org.springframework.web.bind.annotation.*;
/**
* @Author: zhangchq
* @CreateTime: 2019年08月09日 13时53分
* @Decription:
*/
@Api(tags = "01、登录")
@RestController
@RequestMapping("/atlp/common/loginApi")
public class AtlpLoginController extends BaseController {
@Reference
private IAtlpLoginService iLoginService;
@ApiOperation("01、账号密码登录")
@GetMapping("/loginWithCodeAndPwd")
@AtlpLogAspect
public UserInfo doLogin(@ModelAttribute LoginPojo loginPojo) throws BusinessException {
return iLoginService.doLogin(loginPojo);
}
@ApiOperation("02、token登录")
@GetMapping("/loginWithToken/{token}")
@AtlpLogAspect
public UserInfo doLoginWithToken(@PathVariable("token") String token) throws BusinessException {
return iLoginService.doLoginWithToken(token);
}
}
