java 中prehandle,如何在Spring拦截器preHandle方法得到控制方法名

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

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

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.

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.

}

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.

For instance:

@Around("execution (* com.yourpackages.controllers.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")

public Object doSomething(ProceedingJoinPoint pjp){

pjp.getSignature().getDeclaringType().getName();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值