您现在的位置是:主页 > news > 沈阳网站建设建设公司排名/seo推广的网站和平台有哪些

沈阳网站建设建设公司排名/seo推广的网站和平台有哪些

admin2025/5/6 8:32:23news

简介沈阳网站建设建设公司排名,seo推广的网站和平台有哪些,招聘信息网站,2022实时热点新闻事件SpringAOP和注解的使用-配置全局日志自定义注解切面类要使用自定义注解必须首先在配置文件中开启注解模式 <!--开启springMVC注解模式--><!--面向切面配置--><context:component-scan base-package"com.test"/><mvc:annotation-driven />&l…

沈阳网站建设建设公司排名,seo推广的网站和平台有哪些,招聘信息网站,2022实时热点新闻事件SpringAOP和注解的使用-配置全局日志自定义注解切面类要使用自定义注解必须首先在配置文件中开启注解模式 <!--开启springMVC注解模式--><!--面向切面配置--><context:component-scan base-package"com.test"/><mvc:annotation-driven />&l…

SpringAOP和注解的使用-配置全局日志

  • 自定义注解
  • 切面类

要使用自定义注解必须首先在配置文件中开启注解模式

<!--开启springMVC注解模式--><!--面向切面配置--><context:component-scan base-package="com.test"/><mvc:annotation-driven  /><aop:aspectj-autoproxy /><aop:config proxy-target-class="true"/>

自定义注解

package com.test.aspect;import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {/*模块*/String title() default "";/*功能*/String action() default "";
}

切面类

package com.test.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;/*** @ClassName LogAspect* @Description* @Author 周志强* @Date 2021/5/12 12:27* @Version 1.0*/
@Aspect
@Component("logAspect")
public class LogAspect {private static Logger log= LoggerFactory.getLogger(LogAspect.class);//配置切入点@Pointcut("@annotation(com.test.aspect.Log)")public void logPointCut(){}//后置通知 用于拦截操作,在方法返回后执行@AfterReturning(pointcut = "logPointCut()")public void doBefore(JoinPoint joinPoint){handelLog(joinPoint,null);}//拦截异常操作,有异常时执行@AfterThrowing(value = "logPointCut()",throwing = "e")public void doAfter(JoinPoint joinPoint,Exception e){handelLog(joinPoint,e);}public void handelLog(JoinPoint joinpoint,Exception e){try {Log annotationLog = getAnnotationLog(joinPoint);if (annotationLog == null) {return;}String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();String action = annotationLog.action();String title = annotationLog.title();//打印日志,如有需要还可以存入数据库logger.info(">>>>>>>>>>>>>模块名称:{}",title);logger.info(">>>>>>>>>>>>>操作名称:{}",action);logger.info(">>>>>>>>>>>>>类名:{}",className);logger.info(">>>>>>>>>>>>>方法名:{}",methodName);}catch (Exception exception){logger.error("=前置通知异常=");logger.error("异常信息:{}",e.getMessage());e.printStackTrace();}}private static 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;}
}