如何在Spring拦截器preHandle方法得到控制方法名 [英] how to get controller method name in Spring Interceptor preHandle method

查看:5582
本文介绍了如何在Spring拦截器preHandle方法得到控制方法名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序基于Spring MVC和我使用 @Controller 注释来配置控制器春天的安全性。

In my application based on spring mvc and spring security I am using @Controller annotation to configure controller.

我已经配置的春季处理器拦截器 preHandle()的方法,我想获得方法名这将是由拦截器调用。

I have configured Spring Handler Interceptor and in preHandle() method , I want to get method name which is going to be call by interceptor.

我想在 preHandle控制器方法定义的自定义注释() 的HandlerInterceptor 的方法,以便我可以通过为特定方法记录活动管理。

I want to get custom annotation defined on controller method in preHandle() method of HandlerInterceptor so that I can manage by logging activity for that particular method.

请在我的申请要求,code看看

Please have a look at my application requirement and code

@Controller
public class ConsoleUserManagementController{
@RequestMapping(value = CONSOLE_NAMESPACE + "/account/changePassword.do", method = RequestMethod.GET)
@doLog(true)
public ModelAndView showChangePasswordPage() {
    String returnView = USERMANAGEMENT_NAMESPACE + "/account/ChangePassword";
    ModelAndView mavChangePassword = new ModelAndView(returnView);
    LogUtils.logInfo("Getting Change Password service prerequisit attributes");
    mavChangePassword.getModelMap().put("passwordModel", new PasswordModel());
    return mavChangePassword;
}
}


public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   // here I want the controller method name(i.e showChangePasswordPage() 
   // for /account/changePassword.do url ) to be called and that method annotation 
   // (i.e doLog() ) so that by viewing annotation , I can manage whether for that 
   // particular controller method, whether to enable logging or not.
}

我使用Spring 3.0的在我的应用

I am using SPRING 3.0 in my application

推荐答案

不知道处理程序拦截,但你可以尝试使用方面,并为所有的控制器方法的一般的拦截器。

Don't know about the Handler interceptor, but you could try to use Aspects and create a general interceptor for all your controller methods.

使用方面,它会很容易访问您的连接点方法名。

Using aspects, it would be easy to access your joinpoint method name.

您可以注入请求对象的方面或使用里面:

You can inject the request object inside your aspect or use:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

要从你的建议方法来检索它。

To retrieve it from your advice method.

例如:

@Around("execution (* com.yourpackages.controllers.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object doSomething(ProceedingJoinPoint pjp){
 pjp.getSignature().getDeclaringType().getName();
}

这篇关于如何在Spring拦截器preHandle方法得到控制方法名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆