java spring3注释basec控制器与日志记录

发布于 2024-12-23 09:52:36 字数 409 浏览 3 评论 0原文

我正在使用 java 和 spring3 我有以下控制器的服务方法

@Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    }

我有以下方法为每个方法执行日志记录,我想为每个服务方法调用此方法

public void performLog(HttpServletRequest request){
//process params and log msg
log.debug()
}

请告诉我如何在服务方法之后自动调用 PerformLog(request) 方法?

I am using java with spring3 i have following controller's service method

@Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    }

I have the following method which performs logging for every method, i want to call this method for every service method

public void performLog(HttpServletRequest request){
//process params and log msg
log.debug()
}

Please tell me how can i call performLog(request) method after service method automatically ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-12-30 09:52:36

您必须使用 Spring AOP。使用 @Before 注释指定必要的切入点。将此方法放置在用 @Aspect 注释的类中。 类似于切入点示例的内容

@Aspect
public class BeforeExample {

  @Pointcut("execution(ModelAndView com.xyz.myapp.MyController.handleRequest(..))")
  public void performLog() {
  // ...
  }

  @Before("execution(* com.xyz.myapp.MyController.*(..))")
  public void performLogAll() {
  // ...
  }
}

可以找到 此处

检查此处了解更多信息

You would have to use Spring AOP for it . Use an @Before annotation specifying the necessary pointcut . Place this method in a class annotated with @Aspect . Something similar to

@Aspect
public class BeforeExample {

  @Pointcut("execution(ModelAndView com.xyz.myapp.MyController.handleRequest(..))")
  public void performLog() {
  // ...
  }

  @Before("execution(* com.xyz.myapp.MyController.*(..))")
  public void performLogAll() {
  // ...
  }
}

Pointcut samples can be found here

Check here for more info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文