spring-基于SpringBoot使用AOP技术实现操作日志管理
基于SpringBoot使用AOP技术实现操作日志管理
置顶 2019-10-23 15:51:00 伍婷 阅读数 1481更多
分类专栏: Spring Boot 随笔札记
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
操作日志对于程序员或管理员而言,可以快速定位到系统中相关的操作,而对于操作日志的管理的实现不能对正常业务实现进行影响,否则即不满足单一原则,也会导致后续代码维护困难,因此我们考虑使用AOP切面技术来实现对日志管理的实现。
文章大致内容:
1、基本概念
2、基本应用
3、日志管理实战
对这几部分理解了,会对AOP的应用应该很轻松。
一、基本概念
项目 | 描述 |
---|---|
Aspect(切面) | 跨越多个类的关注点的模块化,切面是通知和切点的结合。通知和切点共同定义了切面的全部内容——它是什么,在何时和何处完成其功能。事务处理和日志处理可以理解为切面 |
Join point(连接点) | 程序执行过程中的一个点,如方法的执行或异常的处理 |
Advice(通知) | 切面在特定连接点上采取的动作 |
Pointcut(切点) | 匹配连接点的断言。通知与切入点表达式相关联,并在切入点匹配的任何连接点上运行(例如,具有特定名称的方法的执行)。切入点表达式匹配的连接点概念是AOP的核心,Spring默认使用AspectJ切入点表达式语言 |
Introduction(引用) | 为类型声明其他方法或字段。Spring AOP允许您向任何建议的对象引入新的接口(和相应的实现)。例如,您可以使用介绍使bean实现IsModified接口,以简化缓存 |
Target object(目标) | 由一个或多个切面通知的对象。也称为“通知对象”。由于Spring AOP是通过使用运行时代理实现的,所以这个对象始终是代理对象 |
AOP proxy(代理) | AOP框架为实现切面契约(通知方法执行等)而创建的对象。在Spring框架中,AOP代理是JDK动态代理或CGLIB代理 |
Weaving(织入) | 织入是将通知添加对目标类具体连接点上的过程,可以在编译时(例如使用AspectJ编译器)、加载时或运行时完成 |
Spring切面可以应用5种类型的通知:
- 前置通知(Before):在目标方法被调用之前调用通知
- 后置通知(After):在目标方法完成之后调用通知(无论是正常还是异常退出)
- 返回通知(After-returning):在目标方法成功执行之后调用通知
- 异常通知(After-throwing):在目标方法抛出异常后调用通知
- 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为
其执行的顺序为:
后续的基本应用,会将 环绕通知、前置通知、后置通知、返回通知、异常通知进行实现,并演示其执行顺序。
二、基本应用
声明通知
大家可以将下面的代码复制出来,验证上面的执行顺序。
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Aspect</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> Test <span style="color:#999999">{</span>
<span style="color:#c678dd">private</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">int</span> step <span style="color:#669900">=</span> <span style="color:#98c379">0</span><span style="color:#999999">;</span>
<span style="color:#999999">@Pointcut</span><span style="color:#999999">(</span><span style="color:#669900">"@annotation(com.chenyanwu.erp.erpframework.annotation.Log)"</span><span style="color:#999999">)</span> <span style="color:#5c6370">// the pointcut expression</span>
<span style="color:#c678dd">private</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">operation</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span><span style="color:#999999">}</span>
<span style="color:#999999">@Before</span><span style="color:#999999">(</span><span style="color:#669900">"operation()"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doBeforeTask</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">++</span>step <span style="color:#669900">+</span> <span style="color:#669900">" 前置通知"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">@After</span><span style="color:#999999">(</span><span style="color:#669900">"operation()"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doAfterTask</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">++</span>step <span style="color:#669900">+</span> <span style="color:#669900">" 后置通知"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">@AfterReturning</span><span style="color:#999999">(</span>pointcut <span style="color:#669900">=</span> <span style="color:#669900">"operation()"</span><span style="color:#999999">,</span> returning <span style="color:#669900">=</span> <span style="color:#669900">"retVal"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doAfterReturnningTask</span><span style="color:#999999">(</span>Object retVal<span style="color:#999999">)</span> <span style="color:#999999">{</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">++</span>step <span style="color:#669900">+</span> <span style="color:#669900">" 返回通知,返回值为:"</span> <span style="color:#669900">+</span> retVal<span style="color:#999999">.</span><span style="color:#61aeee">toString</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">@AfterThrowing</span><span style="color:#999999">(</span>pointcut <span style="color:#669900">=</span> <span style="color:#669900">"operation()"</span><span style="color:#999999">,</span> throwing <span style="color:#669900">=</span> <span style="color:#669900">"ex"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doAfterThrowingTask</span><span style="color:#999999">(</span>Exception ex<span style="color:#999999">)</span> <span style="color:#999999">{</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">++</span>step <span style="color:#669900">+</span> <span style="color:#669900">" 异常通知,异常信息为:"</span> <span style="color:#669900">+</span> ex<span style="color:#999999">.</span><span style="color:#61aeee">getMessage</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#5c6370">/**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程ProceedingJoinPoint类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值即目标方法的返回值
*/</span>
<span style="color:#5c6370">//@Around("operation()")</span>
<span style="color:#c678dd">public</span> Object <span style="color:#61aeee">doAroundTask</span><span style="color:#999999">(</span>ProceedingJoinPoint pjp<span style="color:#999999">)</span> <span style="color:#999999">{</span>
String methodname <span style="color:#669900">=</span> pjp<span style="color:#999999">.</span><span style="color:#61aeee">getSignature</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Object result <span style="color:#669900">=</span> null<span style="color:#999999">;</span>
<span style="color:#c678dd">try</span> <span style="color:#999999">{</span>
<span style="color:#5c6370">// 前置通知</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"目标方法"</span> <span style="color:#669900">+</span> methodname <span style="color:#669900">+</span> <span style="color:#669900">"开始,参数为"</span> <span style="color:#669900">+</span> Arrays<span style="color:#999999">.</span><span style="color:#61aeee">asList</span><span style="color:#999999">(</span>pjp<span style="color:#999999">.</span><span style="color:#61aeee">getArgs</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 执行目标方法</span>
result <span style="color:#669900">=</span> pjp<span style="color:#999999">.</span><span style="color:#61aeee">proceed</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 返回通知</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"目标方法"</span> <span style="color:#669900">+</span> methodname <span style="color:#669900">+</span> <span style="color:#669900">"执行成功,返回"</span> <span style="color:#669900">+</span> result<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span> <span style="color:#c678dd">catch</span> <span style="color:#999999">(</span>Throwable e<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#5c6370">// 异常通知</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"目标方法"</span> <span style="color:#669900">+</span> methodname <span style="color:#669900">+</span> <span style="color:#669900">"抛出异常: "</span> <span style="color:#669900">+</span> e<span style="color:#999999">.</span><span style="color:#61aeee">getMessage</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#5c6370">// 后置通知</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"目标方法"</span> <span style="color:#669900">+</span> methodname <span style="color:#669900">+</span> <span style="color:#669900">"结束"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">return</span> result<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
其中需要注意的是切入点:@Pointcut的表达式
格式:
execution(modifiers-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包
<span style="color:#000000"><code class="language-xml"><span style="color:#5c6370"><!-- aop依赖 --></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-aop<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span>
- 1
- 2
- 3
- 4
- 5
2、自定义注解
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Target</span><span style="color:#999999">(</span>ElementType<span style="color:#999999">.</span>METHOD<span style="color:#999999">)</span>
<span style="color:#999999">@Retention</span><span style="color:#999999">(</span>RetentionPolicy<span style="color:#999999">.</span>RUNTIME<span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> @<span style="color:#c678dd">interface</span> Log <span style="color:#999999">{</span>
String <span style="color:#61aeee">value</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#c678dd">default</span> <span style="color:#669900">""</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
3、实现切面
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Aspect</span>
<span style="color:#999999">@Order</span><span style="color:#999999">(</span><span style="color:#98c379">5</span><span style="color:#999999">)</span>
<span style="color:#999999">@Component</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> LogAspect <span style="color:#999999">{</span>
<span style="color:#c678dd">private</span> Logger logger <span style="color:#669900">=</span> LoggerFactory<span style="color:#999999">.</span><span style="color:#61aeee">getLogger</span><span style="color:#999999">(</span>LogAspect<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">@Autowired</span>
<span style="color:#c678dd">private</span> ErpLogService logService<span style="color:#999999">;</span>
<span style="color:#999999">@Autowired</span>
ObjectMapper objectMapper<span style="color:#999999">;</span>
<span style="color:#c678dd">private</span> ThreadLocal<span style="color:#61aeee"><span style="color:#999999"><</span>Date<span style="color:#999999">></span></span> startTime <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ThreadLocal<span style="color:#61aeee"><span style="color:#999999"><</span>Date<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">@Pointcut</span><span style="color:#999999">(</span><span style="color:#669900">"@annotation(com.chenyanwu.erp.erpframework.annotation.Log)"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">pointcut</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#999999">}</span>
<span style="color:#5c6370">/**
* 前置通知,在Controller层操作前拦截
*
* @param joinPoint 切入点
*/</span>
<span style="color:#999999">@Before</span><span style="color:#999999">(</span><span style="color:#669900">"pointcut()"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doBefore</span><span style="color:#999999">(</span>JoinPoint joinPoint<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#5c6370">// 获取当前调用时间</span>
startTime<span style="color:#999999">.</span><span style="color:#61aeee">set</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> Date<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#5c6370">/**
* 正常情况返回
*
* @param joinPoint 切入点
* @param rvt 正常结果
*/</span>
<span style="color:#999999">@AfterReturning</span><span style="color:#999999">(</span>pointcut <span style="color:#669900">=</span> <span style="color:#669900">"pointcut()"</span><span style="color:#999999">,</span> returning <span style="color:#669900">=</span> <span style="color:#669900">"rvt"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doAfter</span><span style="color:#999999">(</span>JoinPoint joinPoint<span style="color:#999999">,</span> Object rvt<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> Exception <span style="color:#999999">{</span>
<span style="color:#61aeee">handleLog</span><span style="color:#999999">(</span>joinPoint<span style="color:#999999">,</span> null<span style="color:#999999">,</span> rvt<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#5c6370">/**
* 异常信息拦截
*
* @param joinPoint
* @param e
*/</span>
<span style="color:#999999">@AfterThrowing</span><span style="color:#999999">(</span>pointcut <span style="color:#669900">=</span> <span style="color:#669900">"pointcut()"</span><span style="color:#999999">,</span> throwing <span style="color:#669900">=</span> <span style="color:#669900">"e"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">doAfter</span><span style="color:#999999">(</span>JoinPoint joinPoint<span style="color:#999999">,</span> Exception e<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> Exception <span style="color:#999999">{</span>
<span style="color:#61aeee">handleLog</span><span style="color:#999999">(</span>joinPoint<span style="color:#999999">,</span> e<span style="color:#999999">,</span> null<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">@Async</span>
<span style="color:#c678dd">private</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">handleLog</span><span style="color:#999999">(</span><span style="color:#c678dd">final</span> JoinPoint joinPoint<span style="color:#999999">,</span> <span style="color:#c678dd">final</span> Exception e<span style="color:#999999">,</span> Object rvt<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> Exception<span style="color:#999999">{</span>
<span style="color:#5c6370">// 获得注解</span>
Method method <span style="color:#669900">=</span> <span style="color:#61aeee">getMethod</span><span style="color:#999999">(</span>joinPoint<span style="color:#999999">)</span><span style="color:#999999">;</span>
Log log <span style="color:#669900">=</span> <span style="color:#61aeee">getAnnotationLog</span><span style="color:#999999">(</span>method<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>log <span style="color:#669900">==</span> null<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">return</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
Date now <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Date<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 操作数据库日志表</span>
ErpLog erpLog <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ErpLog<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setErrorCode</span><span style="color:#999999">(</span><span style="color:#98c379">0</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setIsDeleted</span><span style="color:#999999">(</span><span style="color:#98c379">0</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 请求信息</span>
HttpServletRequest request <span style="color:#669900">=</span> ToolUtil<span style="color:#999999">.</span><span style="color:#61aeee">getRequest</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setType</span><span style="color:#999999">(</span>ToolUtil<span style="color:#999999">.</span><span style="color:#61aeee">isAjaxRequest</span><span style="color:#999999">(</span>request<span style="color:#999999">)</span> <span style="color:#669900">?</span> <span style="color:#669900">"Ajax请求"</span> <span style="color:#669900">:</span> <span style="color:#669900">"普通请求"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setTitle</span><span style="color:#999999">(</span>log<span style="color:#999999">.</span><span style="color:#61aeee">value</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setHost</span><span style="color:#999999">(</span>request<span style="color:#999999">.</span><span style="color:#61aeee">getRemoteHost</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setUri</span><span style="color:#999999">(</span>request<span style="color:#999999">.</span><span style="color:#61aeee">getRequestURI</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">toString</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// erpLog.setHeader(request.getHeader(HttpHeaders.USER_AGENT));</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setHttpMethod</span><span style="color:#999999">(</span>request<span style="color:#999999">.</span><span style="color:#61aeee">getMethod</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setClassMethod</span><span style="color:#999999">(</span>joinPoint<span style="color:#999999">.</span><span style="color:#61aeee">getSignature</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">getDeclaringTypeName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#669900">+</span> <span style="color:#669900">"."</span> <span style="color:#669900">+</span> joinPoint<span style="color:#999999">.</span><span style="color:#61aeee">getSignature</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 请求的方法参数值</span>
Object<span style="color:#999999">[</span><span style="color:#999999">]</span> args <span style="color:#669900">=</span> joinPoint<span style="color:#999999">.</span><span style="color:#61aeee">getArgs</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 请求的方法参数名称</span>
LocalVariableTableParameterNameDiscoverer u
<span style="color:#669900">=</span> <span style="color:#c678dd">new</span> LocalVariableTableParameterNameDiscoverer<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
String<span style="color:#999999">[</span><span style="color:#999999">]</span> paramNames <span style="color:#669900">=</span> u<span style="color:#999999">.</span><span style="color:#61aeee">getParameterNames</span><span style="color:#999999">(</span>method<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>args <span style="color:#669900">!=</span> null <span style="color:#669900">&&</span> paramNames <span style="color:#669900">!=</span> null<span style="color:#999999">)</span> <span style="color:#999999">{</span>
StringBuilder params <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> StringBuilder<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
params <span style="color:#669900">=</span> <span style="color:#61aeee">handleParams</span><span style="color:#999999">(</span>params<span style="color:#999999">,</span> args<span style="color:#999999">,</span> Arrays<span style="color:#999999">.</span><span style="color:#61aeee">asList</span><span style="color:#999999">(</span>paramNames<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setParams</span><span style="color:#999999">(</span>params<span style="color:#999999">.</span><span style="color:#61aeee">toString</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
String retString <span style="color:#669900">=</span> JsonUtil<span style="color:#999999">.</span><span style="color:#61aeee">bean2Json</span><span style="color:#999999">(</span>rvt<span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setResponseValue</span><span style="color:#999999">(</span>retString<span style="color:#999999">.</span><span style="color:#61aeee">length</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#669900">></span> <span style="color:#98c379">5000</span> <span style="color:#669900">?</span> JsonUtil<span style="color:#999999">.</span><span style="color:#61aeee">bean2Json</span><span style="color:#999999">(</span><span style="color:#669900">"请求参数数据过长不与显示"</span><span style="color:#999999">)</span> <span style="color:#669900">:</span> retString<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>e <span style="color:#669900">!=</span> null<span style="color:#999999">)</span> <span style="color:#999999">{</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setErrorCode</span><span style="color:#999999">(</span><span style="color:#98c379">1</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setErrorMessage</span><span style="color:#999999">(</span>e<span style="color:#999999">.</span><span style="color:#61aeee">getMessage</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
Date stime <span style="color:#669900">=</span> startTime<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setStartTime</span><span style="color:#999999">(</span>stime<span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setEndTime</span><span style="color:#999999">(</span>now<span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setExecuteTime</span><span style="color:#999999">(</span>now<span style="color:#999999">.</span><span style="color:#61aeee">getTime</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#669900">-</span> stime<span style="color:#999999">.</span><span style="color:#61aeee">getTime</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setUsername</span><span style="color:#999999">(</span>MySysUser<span style="color:#999999">.</span><span style="color:#61aeee">loginName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
HashMap<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">,</span> String<span style="color:#999999">></span></span> browserMap <span style="color:#669900">=</span> ToolUtil<span style="color:#999999">.</span><span style="color:#61aeee">getOsAndBrowserInfo</span><span style="color:#999999">(</span>request<span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setOperatingSystem</span><span style="color:#999999">(</span>browserMap<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span><span style="color:#669900">"os"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setBrower</span><span style="color:#999999">(</span>browserMap<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span><span style="color:#669900">"browser"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
erpLog<span style="color:#999999">.</span><span style="color:#61aeee">setId</span><span style="color:#999999">(</span>IdUtil<span style="color:#999999">.</span><span style="color:#61aeee">simpleUUID</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
logService<span style="color:#999999">.</span><span style="color:#61aeee">insertSelective</span><span style="color:#999999">(</span>erpLog<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#5c6370">/**
* 是否存在注解,如果存在就获取
*/</span>
<span style="color:#c678dd">private</span> Log <span style="color:#61aeee">getAnnotationLog</span><span style="color:#999999">(</span>Method method<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>method <span style="color:#669900">!=</span> null<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">return</span> method<span style="color:#999999">.</span><span style="color:#61aeee">getAnnotation</span><span style="color:#999999">(</span>Log<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">return</span> null<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">private</span> Method <span style="color:#61aeee">getMethod</span><span style="color:#999999">(</span>JoinPoint joinPoint<span style="color:#999999">)</span> <span style="color:#999999">{</span>
Signature signature <span style="color:#669900">=</span> joinPoint<span style="color:#999999">.</span><span style="color:#61aeee">getSignature</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
MethodSignature methodSignature <span style="color:#669900">=</span> <span style="color:#999999">(</span>MethodSignature<span style="color:#999999">)</span> signature<span style="color:#999999">;</span>
Method method <span style="color:#669900">=</span> methodSignature<span style="color:#999999">.</span><span style="color:#61aeee">getMethod</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>method <span style="color:#669900">!=</span> null<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">return</span> method<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">return</span> null<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">private</span> StringBuilder <span style="color:#61aeee">handleParams</span><span style="color:#999999">(</span>StringBuilder params<span style="color:#999999">,</span> Object<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">,</span> List paramNames<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> JsonProcessingException <span style="color:#999999">{</span>
<span style="color:#c678dd">for</span> <span style="color:#999999">(</span><span style="color:#c678dd">int</span> i <span style="color:#669900">=</span> <span style="color:#98c379">0</span><span style="color:#999999">;</span> i <span style="color:#669900"><</span> args<span style="color:#999999">.</span>length<span style="color:#999999">;</span> i<span style="color:#669900">++</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>args<span style="color:#999999">[</span>i<span style="color:#999999">]</span> <span style="color:#c678dd">instanceof</span> Map<span style="color:#999999">)</span> <span style="color:#999999">{</span>
Set set <span style="color:#669900">=</span> <span style="color:#999999">(</span><span style="color:#999999">(</span>Map<span style="color:#999999">)</span> args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">keySet</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List list <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ArrayList<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List paramList <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ArrayList<span style="color:#669900"><</span><span style="color:#669900">></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">for</span> <span style="color:#999999">(</span>Object key <span style="color:#669900">:</span> set<span style="color:#999999">)</span> <span style="color:#999999">{</span>
list<span style="color:#999999">.</span><span style="color:#61aeee">add</span><span style="color:#999999">(</span><span style="color:#999999">(</span><span style="color:#999999">(</span>Map<span style="color:#999999">)</span> args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span>key<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
paramList<span style="color:#999999">.</span><span style="color:#61aeee">add</span><span style="color:#999999">(</span>key<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">return</span> <span style="color:#61aeee">handleParams</span><span style="color:#999999">(</span>params<span style="color:#999999">,</span> list<span style="color:#999999">.</span><span style="color:#61aeee">toArray</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span> paramList<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span> <span style="color:#c678dd">else</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">if</span> <span style="color:#999999">(</span>args<span style="color:#999999">[</span>i<span style="color:#999999">]</span> <span style="color:#c678dd">instanceof</span> Serializable<span style="color:#999999">)</span> <span style="color:#999999">{</span>
Class<span style="color:#669900"><</span><span style="color:#669900">?</span><span style="color:#669900">></span> aClass <span style="color:#669900">=</span> args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#61aeee">getClass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">try</span> <span style="color:#999999">{</span>
aClass<span style="color:#999999">.</span><span style="color:#61aeee">getDeclaredMethod</span><span style="color:#999999">(</span><span style="color:#669900">"toString"</span><span style="color:#999999">,</span> <span style="color:#c678dd">new</span> Class<span style="color:#999999">[</span><span style="color:#999999">]</span><span style="color:#999999">{</span>null<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#5c6370">// 如果不抛出NoSuchMethodException 异常则存在 toString 方法 ,安全的writeValueAsString ,否则 走 Object的 toString方法</span>
params<span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">" "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>paramNames<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span>i<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">": "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>objectMapper<span style="color:#999999">.</span><span style="color:#61aeee">writeValueAsString</span><span style="color:#999999">(</span>args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span> <span style="color:#c678dd">catch</span> <span style="color:#999999">(</span>NoSuchMethodException e<span style="color:#999999">)</span> <span style="color:#999999">{</span>
params<span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">" "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>paramNames<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span>i<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">": "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>objectMapper<span style="color:#999999">.</span><span style="color:#61aeee">writeValueAsString</span><span style="color:#999999">(</span>args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#61aeee">toString</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span> <span style="color:#c678dd">else</span> <span style="color:#c678dd">if</span> <span style="color:#999999">(</span>args<span style="color:#999999">[</span>i<span style="color:#999999">]</span> <span style="color:#c678dd">instanceof</span> MultipartFile<span style="color:#999999">)</span> <span style="color:#999999">{</span>
MultipartFile file <span style="color:#669900">=</span> <span style="color:#999999">(</span>MultipartFile<span style="color:#999999">)</span> args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">;</span>
params<span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">" "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>paramNames<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span>i<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">": "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>file<span style="color:#999999">.</span><span style="color:#61aeee">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span> <span style="color:#c678dd">else</span> <span style="color:#999999">{</span>
params<span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">" "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>paramNames<span style="color:#999999">.</span><span style="color:#61aeee">get</span><span style="color:#999999">(</span>i<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span><span style="color:#669900">": "</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">append</span><span style="color:#999999">(</span>args<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">return</span> params<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
4、对应代码添加注解
<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Log</span><span style="color:#999999">(</span><span style="color:#669900">"新增学生"</span><span style="color:#999999">)</span>
<span style="color:#999999">@RequestMapping</span><span style="color:#999999">(</span>value <span style="color:#669900">=</span> <span style="color:#669900">"/create"</span><span style="color:#999999">,</span> method <span style="color:#669900">=</span> RequestMethod<span style="color:#999999">.</span>POST<span style="color:#999999">)</span>
<span style="color:#999999">@ResponseBody</span>
<span style="color:#c678dd">public</span> ResultBean<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> <span style="color:#61aeee">create</span><span style="color:#999999">(</span><span style="color:#999999">@RequestBody</span> <span style="color:#999999">@Validated</span> ErpStudent item<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#c678dd">if</span><span style="color:#999999">(</span>service<span style="color:#999999">.</span><span style="color:#61aeee">insertSelective</span><span style="color:#999999">(</span>item<span style="color:#999999">)</span> <span style="color:#669900">==</span> <span style="color:#98c379">1</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#5c6370">// 插入</span>
<span style="color:#61aeee">insertErpSFamilyMember</span><span style="color:#999999">(</span>item<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> ResultBean<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#669900">""</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> ResultBean<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span><span style="color:#999999">(</span>ExceptionEnum<span style="color:#999999">.</span>BUSINESS_ERROR<span style="color:#999999">,</span> <span style="color:#669900">"新增学生异常!"</span><span style="color:#999999">,</span> <span style="color:#669900">"新增失败!"</span><span style="color:#999999">,</span> <span style="color:#669900">""</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
通过对业务进行操作后,会写入数据库,界面查询:
日志管理的完整的代码可以从git上获取:
https://github.com/chyanwu/erp-framework
更多精彩,更多技术请关注:码蚁在线(coding_online)