Java Interceptor如何记录日志

发布时间:2025-07-05 00:15:17 作者:小樊
来源:亿速云 阅读:97

在Java中,拦截器(Interceptor)通常用于在方法调用前后执行一些操作,例如记录日志、验证权限等。要使用拦截器记录日志,你可以使用Java的动态代理或者第三方库(如Spring AOP)来实现。下面是两种方法的示例:

  1. 使用Java动态代理:

首先,创建一个接口和一个实现类:

public interface MyService {
    void doSomething();
}

public class MyServiceImpl implements MyService {
    @Override
    public void doSomething() {
        System.out.println("MyServiceImpl: doSomething");
    }
}

接下来,创建一个日志拦截器类:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class LoggingInterceptor implements InvocationHandler {
    private Object target;

    public LoggingInterceptor(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Before method: " + method.getName());
        Object result = method.invoke(target, args);
        System.out.println("After method: " + method.getName());
        return result;
    }
}

最后,在主类中使用动态代理创建代理对象并调用方法:

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        MyService myService = new MyServiceImpl();
        MyService proxy = (MyService) Proxy.newProxyInstance(
                MyService.class.getClassLoader(),
                new Class<?>[]{MyService.class},
                new LoggingInterceptor(myService)
        );
        proxy.doSomething();
    }
}
  1. 使用Spring AOP:

首先,在pom.xml文件中添加Spring AOP依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

接下来,创建一个切面类:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    @Before("execution(* com.example.MyService.*(..))")
    public void beforeMethod(JoinPoint joinPoint) {
        System.out.println("Before method: " + joinPoint.getSignature().getName());
    }

    @After("execution(* com.example.MyService.*(..))")
    public void afterMethod(JoinPoint joinPoint) {
        System.out.println("After method: " + joinPoint.getSignature().getName());
    }
}

然后,在主类中使用Spring AOP:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(Main.class, args);
        MyService myService = applicationContext.getBean(MyService.class);
        myService.doSomething();
    }
}

这样,在调用MyService的方法时,日志拦截器会自动记录方法的执行前后的日志。

推荐阅读:
  1. Java中inteceptor拦截器的原理是什么
  2. 在java项目中如何使用Struts2拦截器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:Ansible与云服务如何结合使用

下一篇:Java Interceptor有哪些应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》