如何在Spring Interceptor preHandle方法中获取控制器方法名称


问题内容

在基于spring mvc和spring security的应用程序中,我使用@Controller注释来配置控制器。

我已经配置了 Spring Handler Interceptor,
并且在preHandle()method中,我想要获取将由拦截器调用的方法名称。

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

请看看我的申请要求和代码

@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


问题答案:

不了解Handler拦截器,但是您可以尝试使用Aspects并为所有控制器方法创建通用拦截器。

使用方面,可以很容易地访问您的联接点方法名称。

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

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

从您的建议方法中检索它。

例如:

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