本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下
通过aop的切面方式实现日志
通切面拦截所有指定包下的所有方法
@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspect1{
Logger logger = LoggerFactory.getLogger(LogAspect.class);
/**
* 拦截切点
*/
@Pointcut(\"execution(*xx.xx.controller.*.*(..))\")
private void logPointCut() {
logger.info(\"进入注解拦截\");
}
//前置通知,在方法之前通知
@Before(value = \"logPointCut()\")
public void before(JoinPoint jp) {
logger.info(\"方法调用前:\" + \"类名:\" + jp.getTarget().getClass().getName() + \"方法名 :\" + jp.getSignature().getName());
}
//后置通知
@After(value = \"logPointCut()\")
public void doAfter(JoinPoint jp) {
logger.info(\"方法调用结束:\" + \"类名:\" + jp.getTarget().getClass().getName() + \"方法名 :\" + jp.getSignature().getName());
}
//环绕通知
@Around(\"logPointCut()\")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
logger.info(\"方法开始调用》》》》》》\");
Object retVal = pjp.proceed();
logger.info(\"方法调用结束》》》》》》\");
return retVal;
}
//返回通知
@AfterReturning(pointcut = \"logPointCut()\")
public void doAfterReturning(JoinPoint jp) {
logger.info(\"写入日志\");
}
//异常通知
@AfterThrowing(pointcut = \"logPointCut()\", throwing = \"ex\")
public void doAfterThrowing(JoinPoint jp, Throwable ex) {
logger.info(\"方法异常,异常信息:\" + ex.getMessage());
}
}
拦截自定义注解
定义一个日志注解,在所有要需要记录的方法上加盖注解即可被后续的aop拦截处理
代码如下
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
/**
* 日志主题
*/
public String title() default \"\";
/**
* 操作具体业务
*/
public String business() default \"\";
/**
* 是否保留请求参数
*/
public boolean isSaveRequestData() default false;
/**
* 日志的类别,主要用于日志的分开记录和查询
**/
LogType logType() default LogType.LOGIN;
}
拦截切面的实现
@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspect {
Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Autowired
private ServiceDemo serviceDemo;
/**
* 拦截切点
*/
@Pointcut(\"@annotation(moling.evolution.demo.aop.annotation.Log)\")
private void logPointCut() {
}
//前置通知,在方法之前通知
@Before(value = \"logPointCut()\")
public void before(JoinPoint jp) {
logger.info(\"方法调用前:\" + \"类名:\" + jp.getTarget().getClass().getName() + \"方法名 :\" + jp.getSignature().getName());
}
//后置通知
@After(value = \"logPointCut()\")
public void doAfter(JoinPoint jp) {
logger.info(\"方法参数:{}\", jp.getArgs());
logger.info(\" {} || {}\", jp.getStaticPart().getId(), jp.getStaticPart().getSourceLocation());
jp.getStaticPart().getId();
logger.info(\"方法调用结束:\" + \"类名:\" + jp.getTarget().getClass().getName() + \"方法名 :\" + jp.getSignature().getName());
}
//环绕通知
@Around(\"logPointCut()\")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
logger.info(\"方法开始调用》》》》》》\");
Object retVal = pjp.proceed();
logger.info(\"方法调用结束》》》》》》\");
return retVal;
}
//返回通知
@AfterReturning(pointcut = \"logPointCut()\", returning = \"object\")
public void doAfterReturning(JoinPoint jp, Object object) {
System.out.println(\"返回通知\");
Log log = null;
try {
log = getAnnotationLog(jp);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(object);
System.out.println(log);
if (log != null) {
logger.info(log.title());
logger.info(log.business());
logger.info(log.user());
logger.info(log.isSaveRequestData() + \"\");
} else {
logger.error(\"获取注解信息失败\");
}
serviceDemo.demo();
}
//异常通知
@AfterThrowing(pointcut = \"logPointCut()\", throwing = \"ex\")
public void doAfterThrowing(JoinPoint jp, Throwable ex) {
logger.info(\"方法异常,异常信息:\" + ex.getMessage());
serviceDemo.error();
}
/**
* 是否存在注解,如果存在就获取
*/
private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(Log.class);
}
return null;
}
}
基于事件通知实现日志的记录
- 拦截切面的实现
@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspectContext {
Logger logger = LoggerFactory.getLogger(LogAspectContext.class);
@Autowired
private ApplicationContext applicationContext;
/**
* 拦截切点
*/
@Pointcut(\"@annotation(moling.evolution.demo.aop.annotation.Log)\")
private void logPointCut() {
logger.info(\"进入注解拦截\");
}
//返回通知
@AfterReturning(pointcut = \"logPointCut()\", returning = \"object\")
public void doAfterReturning(JoinPoint jp, Object object) throws Exception {
applicationContext.publishEvent(new LogSuccessEvent(jp, null));
}
//异常通知
@AfterThrowing(pointcut = \"logPointCut()\", throwing = \"ex\")
public void doAfterThrowing(JoinPoint jp, Throwable ex) {
logger.info(\"方法异常,异常信息:\" + ex.getMessage());
applicationContext.publishEvent(new LogSuccessEvent(jp, ex));
}
/**
* 是否存在注解,如果存在就获取
*/
private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(Log.class);
}
return null;
}
}
@Slf4j
@Service
public class ApplicationListener implements org.springframework.context.ApplicationListener<LogSuccessEvent> {
@Autowired
private ServiceDemo demo;
@Override
public void onApplicationEvent(LogSuccessEvent event) {
JoinPoint joinPoint = event.getJp();
Throwable ex = event.getThrowable();
if (ex == null) {
demo.demo();
} else {
demo.error();
}
}
}
@Slf4j
public class LogSuccessEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the component that published the event (never {@code null})
*/
private JoinPoint jp;
private Throwable throwable;
public LogSuccessEvent(Object source, Throwable throwable) {
super(source);
this.throwable = throwable;
this.jp = (JoinPoint) source;
// Log logger = (Log) source;
// log.info(logger.title());
// log.info(logger.business());
// log.info(logger.user());
// log.info(logger.isSaveRequestData() + \"\");
}
public JoinPoint getJp() {
return jp;
}
public Throwable getThrowable() {
return throwable;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!
猪小侠源码-最新源码下载平台 PHP教程 springboot实现基于aop的切面日志 http://www.20zxx.cn/464270/xuexijiaocheng/qes.html
猪小侠源码,优质资源分享网
相关文章
- java非法字符‘\\ufeff‘解决方法 2024-03-11
- Java中单体应用锁的局限性&分布式锁 2024-03-11
- 如何通过php函数解决页面渲染慢的问题? 2024-03-11
- 如何评估php性能优化函数的效果? 2024-03-11
- 如何利用PHP脚本在Linux中进行目录操作 2024-03-11
- 如何通过PHP脚本在Linux中进行系统监测 2024-03-11
- 如何使用php函数来优化表单处理和提交功能? 2024-03-11
- 如何通过PHP脚本在Linux服务器上实现数据加密 2024-03-11
- 如何通过php函数来优化验证码生成和校验? 2024-03-11
- 如何使用php函数来优化多语言支持功能? 2024-03-11
做猪小侠源码的代理,提供一站式服务
如果你不懂得搭建网站或者服务器,小程序,源码之类的怎么办? 第一通过本站学习各种互联网的技术 第二就是联系客服,我帮帮你搭建(当然要收取部分的费用) 第三成为我们的代理,我们提供整套的服务。