
本文共 6803 字,大约阅读时间需要 22 分钟。
AOP��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ������������������������������
AOP���������������
AOP������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Spring Boot������������AOP������������ AspectJ ���������������������
������Spring Boot������������������
������������������AOP������
������������������������������Spring Boot���������������������������������Spring AOP���������������������������������������������������������������������������Spring Boot Starter AOP���������
org.springframework.boot spring-boot-starter-aop
���������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.component;@/component@publicclass WebLogAspect { private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class); // ��������������������������� @Pointcut("execution(public * com.yt.controller.*.*(..))") // ������������������������ public void webLog() {} // ������������������...}
������������������
������������������������������������������������Spring���������������������������������������������������������������������������������������
import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotationAfterReturning;import org.aspectj.lang.annotationAfterThrowing;import org.aspectj.lang.JoinPoint;@ component@publicclass WebLogAspect { // ������������������... @Pointcut("execution(public * com.yt.controller.*.*(..))") public void webLog() {} @Before("webLog()") public void doBefore(JoinPoint joinPoint) { // ��������������������������������� } // ������������������...}
������������������
���������������������������������������������������������������������������
1.-days������������������������������������������������ 2. ������������������������������������������ 3. ������������������������������������
###isodes
���������������������������������������
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.component;@ component@publicclass WebLogAspect { private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class); @Autowired private LogService logService; @Autowired private UserService userService; private String url; private String ip; private Integer port; private User user; @Pointcut("execution(public * com.yt.controller..*.*(..))") public void webLog() {} @Before("webLog()") public void doBefore(JoinPoint joinPoint) { ServletRequestAttributes attributes = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // ch��� ���������GET������������������ if (!request.getMethod().equals("GET")) { logger.info("������URL: " + request.getRequestURL().toString()); url = request.getRequestURI(); ip = request.getRemoteHost(); port = request.getRemotePort(); user = UserService.getCurrentUser(); } } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { Integer logType = LogType.OPERATION.getId(); if (ret instanceof ResultInfo) { ResultInfo result = (ResultInfo) ret; String msg = result.getMsg(); String module = result.getModule(); Integer code = result.getCode(); if (!StringUtils.isBlank(msg)) { logger.info(msg); // ���������������200��������������� if (code != null && code.equals(200)) { logService.save( user, ip, port, msg, logType, module, url); } } } } @AfterThrowing(pointcut = "webLog()", throwing = "e") public void handleException(JoinPoint joinPoint, Throwable e) { if (e != null) { StringBuilder message = new StringBuilder( "���������������������" ); message.append( e.getMessage() ); Logger logger = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringClass()); logger.error( message.toString(), e ); } }}
������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������controller���������������������������������
���������������������
���������������������������������������
������������������������"execution(public * com.yt.controller...(..))" ���������com.yt.controller������������������������������������������
���������������������
������������������������������������������������������������������������������������������������������������������������������������module���������������������������������������������������������������������������������
- ���������������ResultInfo���������������module������
- ���������������������������������������������������������������������������
- ���������������������������������
������
������������������������������������������������Spring Boot������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������AOP������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
