念珠菌-必威app-必威app下载

一、根本概念

Spring切面能够使用5种类型的告诉:

前置告诉(Before):在方针办法被调用之前调用告诉

后置告诉(After):在方针办法完结之后调用告诉(无论是正常仍是反常退出)

回来告诉(After-returning):在方针办法成功履行之后调用告诉

反常告诉(After-throwing):在方针办法抛出反常后调用告诉

盘绕告诉(Around):告诉包裹了被告诉的办法,在被告诉的办法调用之前和调用之后履行自定义的行为

其履行的次序为:

后续的根本使用,会将 盘绕告诉前置告诉后置告诉回来告诉反常告诉进行完成,并演示其履行次序。

二、根本使用

声明告诉

咱们能够将下面的代码仿制出来,验证上面的履行次序。

@Aspect

public class Test {

private static int step = 0;

@Pointcut("@annotation(com.chenyanwu.erp.erpframework.annotation.Log)") // the pointcut expression

private void operation() {}

@Before("operation()")

public void doBeforeTa标签14sk() {

System.out.println(++step + " 前置告诉");

}

@After("operation()")

public void doAfterTask() {

System.out.println(++step + " 后置告诉");

}

@AfterReturning(pointcut = "operation()", returning = "retVal")

public void doAfterReturnningTask(Object retVal) {

System.out.println(++step + " 回来告诉,回来值为:" + retVal.toString());

}

@AfterThrowing(pointcut = "operation()", throwing = "ex")

public void doAfterThrowingTask(Except念珠菌-必威app-必威app下载ion ex) {

System.out.println(++step + " 反常告诉,反常信息为:" + ex.getMessage());

}

/**

* 盘绕告诉需求带着ProceedingJoinPoint类型的参数

* 盘绕告诉类似于动态署理的全过程ProceedingJoinPoint类型的参数可念珠菌-必威app-必威app下载以决议是否履行方针办法

* 且盘绕告诉必须有回来值,回来值即方针办法的回来值

*/

//@Around("operation()")

public Object d标签19oAroundTask(ProceedingJoinPoint pjp) {

String methodname = pjp.getSignature().getName();

Object result = null;

try 标签1{

// 前置告诉

System.ou标签20t.println("方针办法" + methodname + "开端,参数为" + Arrays.asList(pjp.getArgs()));

// 履行方针办法

result = pjp.proceed();

// 回来告诉

System.out.println("方针办法" + methodname + "履行成功,回来" + result);

} catch (Throwable e) {

// 反常告诉

System.out.println("方针办法" + methodname + "抛出反常: " + e.getMessage());

}

// 后置告诉

System.out.println("方针办法" + methodname + "完毕");

return result;

}

}

其间需求留意的是切入点:@Pointcut的表达式

格局:

execution(modifi标签5ers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)

括号中各个pattern别离表明:

  • 修饰符匹配(modifier-pattern?)
  • 回来值匹配(ret-type-pattern)能够为*表明任何回来值,全途径的类名等
  • 类途径匹配(declaring-type-pattern?)
  • 办法名匹配(name-pattern)能够指定办法名 或许 代表一切, set 代表以set最初的一切办法
  • 参数匹配((param-pattern))能够指定详细的参数类型,多个参数间用“,”离隔,各个参数也能够用“”来表明- 匹配恣意类型的参数,如(String)表明匹配一个String参数的办法;(,String) 表明匹配有两个参数的办法,第一个参数能够是恣意类型,而第二个参数是String类型;能够用(…)表明零个或多个恣意参数
  • 反常类型匹配(throws-pattern?)
  • 其间后边跟着“?”的是可选项

示例:

1)execution(* (…))

//表明匹配一切办法

2)execution(public * com. savage.service.UserService.(…))

//表明匹配com.savage.server.UserService中一切的公有办法

3)execution(* com.savage.server….(…))

//表明匹配com.savage.server包及其子包下的一切办法

三、日志办理实战

有了上面根本使用的了解,现在咱们直接就贴代码:

1、依靠的jar包

org.springframework.boot

spring-boot-starter-aop

2、自定义注解

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Log {

String value() default "";

}

3、完成切面

@Aspect

@Order(5)

@Component

public class LogAspect {

private Logger 标签1念珠菌-必威app-必威app下载logger = LoggerFactory.getLogger(LogAspect.class);

@Autowired

private ErpLogService logService;

@Autowired

ObjectMapper objectMapper;

private Th念珠菌-必威app-必威app下载readLocal startTime = new Threa标签14dLocal();

@Pointcut("@annotation(com.chenyanwu.erp.erpframework.annotation.Log)")

public void pointcut() {

}

/**

* 前置告诉,在Controller层操作前阻拦

*

* @param joinPoint 切入点

*/

@Before("pointcut()")

public void doBefore(JoinPoint joinPoint) {

// 获取当时调用时刻

startTime.set(new Date());

}

/**

* 正常状况回来

*

* @param joinPoint 切入点

* @param rvt 正常成果

*/

@AfterReturning(pointcut = "pointcut()", returning = "rvt")

public void doAfter(JoinPoint joinPoint, Object rvt) throws Exception {

handleLog(joinPoint, null, rvt);

}

/**

* 反常信息阻拦

*

* @param joinPoint

* @param e

*/

@AfterThrowing(pointcut = "pointcut()", throwing = "e")

public void doAfter(JoinPoint joinPoint, Exception e) throws Exception {

handleLog(joinPoint, e, null);

}

@Async

private void handleLog(final JoinPoint joinPoint, final Exception e, Object rvt) throws Exception{

// 取得注解

Method method = getMethod(joinPoint);

Log log = getAnno标签5tationLog(method);

if (log == null) {

return;

}

Date now = new Date();

// 操作数据库日志表

ErpLog erpLog = new ErpLog();

erpLog.setErrorCode(0);

erpLog.setIsDeleted(0);

// 恳求信息

HttpServletRequest request = ToolUtil.getRequest();

erpLog.setType(To念珠菌-必威app-必威app下载olUtil.isAjaxRequest(request) ? "Ajax恳求" : "一般恳求");

erpLog.setTitle(log.value());

erpLog.setHost(request.getRemoteHost());

erpLog.setUri(request.getRequestURI().toString());

// erpLog.setHeader(request.getHeader(HttpHeaders.USER_AGENT));

erpLog.setHttpMethod(request.getMethod());

erpLog.setClassMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

// 恳求的办法参数值

Object[] args = joinPoint.getArgs();

// 恳求的办法参数称号

LocalVariableTableParameterNameDiscoverer u

= new LocalVariableTableP标签10arameterNameDiscoverer();

String[] paramNames = u.getParameterNames(method);

if (args != null && paramNames != null) {

StringBuilder params = new StringBuilder念珠菌-必威app-必威app下载();

params = handleParams(params, args, Arrays.asList(paramNames));

erpLog.setParams(params.toString());

}

String retString = JsonUtil.bean2Json(rvt);

erpLog.setResponseValue(retString.length() > 5000 ? JsonUtil.bean2Json("恳求参数数据过长不与显标签5示") : retString);

if (e != null) {

erpLog.setErrorCode(1);

erpLog.setErrorMessage(e.getMessage());

}

Date stime = startTime.get();

erpLog.setStartTime(stime);

erpLog.setEndTime(now);

erpLog.setExecuteTime(now.getTime() - stime.getTime());

e标签11rpLog.setUsername(MySysUser.loginName());

HashMap browserMap = ToolUtil.getOsAndBrowserInfo(request);

erpLog.setOperatingSystem(browserMap.get("os"));

erpLog.setBrower(browserMap.get("browser"));

erpLog.setId(IdUtil.simpleUUID());

logService.insertSelect标签11ive(erpLog);

}

/**

* 是否存在注解,假如存在就获取

*/

private Log getAnnotationLog(Method method) {

if (method != null) {

return method.getAnnotation(Log.class);

}

return null;

}

private Method getMethod(JoinPoint joinPoint) {

Signature signature = joinPoint.getSignature();

MethodSignature methodSignature = (MethodSignature) signature;

Method method = methodSignature.getMethod();

if (method != null) {

return method;

}

return nu标签3ll;

}

private StringBuilder handleParams(StringBuilder params, Object[] args, List paramNames) throws JsonProcessingException {

for (int i = 0; i < args.length; i++) {

if (args[i] instanceof Map) {

Set set = ((Map) args[i]).key念珠菌-必威app-必威app下载Set();

List list = new ArrayList();

List paramList = new ArrayList<>();

for (Object key : set) {

list.add(((Map) args[i]).get(key));

paramList.add(key);

}

return handleParams(params, list.toArray(), paramList);

} else {

if (args[i] instanceof Serializable) {

Class

try {

aClass.getDeclaredMethod("toString", new Class[]{null});

// 假如不抛出NoSuchMethodException 反常则存在 toString 办法 ,安全的writeValueAsString ,不然 走 Object的 toString办法

params.append(" ").append(paramNames.get(i)).append(": ").append(objectMapper.writeValueAsString(args[i]));

} catch (NoSuchMethodException e) {

params.append(" ").append(paramNames.get(i)).append(": ").append(objectMapper.writeValueAsString(args[i].toString()));

}

} else if (args[i] instanceof MultipartFile) {

MultipartFile file = (MultipartFile) args[i];

params标签17.append(" ").append(paramNames.get(i)).append(": ").append(file.getName());

} else {

params.append(" ").append(paramNames.get(i)).append(": ").append(args[i]);

}

}

}

return params;

}

}

4、对应代码增加注解

@Log("新增学生")

@R标签20equestMapping(value = "/create", method = RequestMethod.POST)

@ResponseBody

public ResultBean create(@RequestBody @Validated ErpStudent item) {

if(service.insertSelective(item) == 1) {

// 刺进

insertErpSFamilyMember(item);

return new ResultBean("");

}

return new ResultBean(ExceptionEnum.BUSINESS_ERRO标签19R, "新增学生反常!", "新增失利!", "");

}

经过对事务进行操作后,会写入数据库,界面查询:

日志办理的完好的代码能够从git上获取:
https://github.com/chyanwu/erp-framework


原文链接:https://blog.csdn.net/chyanwu68/article/details/102703001

发表评论

电子邮件地址不会被公开。 必填项已用*标注