AOP实战篇 如何轻松实现日志功能,戳这

[](()一、技术介绍

======================================================================

[](()1.AOP是什么?

=========================================================================

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 --摘自百度百科

[](()二、开始使用

======================================================================

[](()1.代码目录结构

========================================================================

image.png

[](()2.开始编写代码

========================================================================

POM.xml

org.springframework.boot

spring-boot-starter-aop

咱们这里采用SpringBoot 写一个按需注入的方式开启注解,编写一个按需注入的注解

package com.hyh.log.annotation;

import com.hyh.log.config.HyhLogAutoConfiguration;

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

/**

  • 开启日志注解

  • @Author: heyuhua

  • @Date: 2021/1/28 15:32

*/

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Import({HyhLogAutoConfiguration.class})

public @interface EnableHyhLog {

}

自动配置类

package com.hyh.log.config;

import com.hyh.log.aspect.LogAspect;

import com.hyh.log.service.LogService;

import com.hyh.log.service.impl.LogServiceImpl;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

  • Log AutoConfiguration

  • @Author: heyuhua

  • @Date: 2021/1/28 16:02

*/

@ConditionalOnWebApplication

@Configuration(proxyBeanMethods = false)

public class HyhLogAutoConfiguration {

@Bean

public LogAspect logAspect() {

return new LogAspect();

}

@Bean

public LogService logService() {

return new LogServiceImpl();

}

}

自定义日志注解

package com.hyh.log.annotation;

import java.lang.annotation.*;

/**

  • 日志注解

  • @Author: heyuhua

  • @Date: 2021/1/28 16:02

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface HyhLog {

/**

  • 描述

  • @return

*/

String value() default “”;

}

复制代码

日志切面代码编写

package com.hyh.log.aspect;

import com.hyh.log.annotation.HyhLog;

import com.hyh.log.service.LogService;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import javax.annotation.Resource;

/**

  • Log Aspect

  • @Author: heyuhua

  • @Date: 2021/1/28 16:02

*/

@Aspect

public cla 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ss LogAspect {

/**

  • 日志服务

*/

@Resource

private LogService logService;

/**

  • 环绕操作

  • @param point

  • @param hyhLog

  • @return

  • @throws Throwable

*/

@Around(“@annotation(hyhLog)”)

public Object around(ProceedingJoinPoint point, HyhLog hyhLog) throws Throwable {

String className = point.getTarget().getClass().getName();

String methodName = point.getSignature().getName();

logService.info(“【类】:{},【方法】:{}”, className, methodName);

Object obj = point.proceed();

// do something

return obj;

}

/**

  • 前置操作

  • @param point

  • @param hyhLog

  • @return

  • @throws Throwable

*/

@Before(“@annotation(hyhLog)”)

public void before(JoinPoint point, HyhLog hyhLog) throws Throwable {

logService.info(“执行前置操作…” + hyhLog.value());

// do something

}

/**

  • 后置操作

  • @param point

  • @param hyhLog

  • @return

  • @throws Throwable

*/

@After(“@annotation(hyhLog)”)

public void after(JoinPoint point, HyhLog hyhLog) throws Throwable {

logService.info(“执行后置操作…” + hyhLog.value());

// do something

}

}

复制代码

日志服务接口

package com.hyh.log.service;

/**

  • Log 接口

  • @Author: heyuhua

  • @Date: 2021/1/28 15:37

*/

public interface LogService {

/**

  • info

  • @param msg

*/

void info(String msg);

/**

  • info

  • @param msg

  • @param o

*/

void info(String msg, Object o);

/**

  • info

  • @param msg

  • @param throwable

*/


版权声明:本文为m0_66789766原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。