tworzenie niestandardowej adnotacji w wiosennym mvc i pobieranie obiektu httpservletrequest
Chcę utworzyć niestandardową adnotację i umieścić tę adnotację na poziomie metody przy użyciuHttpServletRequest
obiekt. jak dotąd to zrobiłem:
Utworzono adnotację
@Target(value={ElementType.METHOD,ElementType.PARAMETER})
@Retention(value=RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Mapping
public @interface CheckSession{
boolean isAuthenticate() default false;
}
utworzona klasa obsługi
@Component
public class CheckSessionClass implements HandlerMethodReturnValueHandler,HandlerMethodArgumentResolver {
@Override
public Object resolveArgument(MethodParameter arg0,
ModelAndViewContainer arg1, NativeWebRequest arg2,
WebDataBinderFactory arg3) throws Exception {
logger.info("......MY ANNOTATION CALLEDD.....resolveArgument");
return null;
}
@Override
public boolean supportsParameter(MethodParameter arg0) {
logger.info("......MY ANNOTATION CALLEDD.....supportsParameter");
return false;
}
@Override
public void handleReturnValue(Object retutnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
CheckSession annotation;
annotation=returnType.getMethodAnnotation(CheckSession.class);
if(annotation.isAuthenticate()){
logger.info("......got request is aurhenticated..true");
}else{
logger.info("......got request is aurhenticated..false");
}
}
@Override
public boolean supportsReturnType(MethodParameter arg0) {
logger.info("......MY ANNOTAION CALLEDD.....supportsReturnType");
return false;
}
}
stworzył kontroler do wywoływania adnotacji w ten sposób.
@Controller
public class MyController
{
@RequestMapping(method={RequestMethod.GET, RequestMethod.POST})
@CheckSession(isAuthenticate=true)
public ResponseEntity<String> mymethod (HttpServletRequest request)
{
///my code here....
}
}
Mój plik applicationContext.xml jest skonfigurowany jako automatyczne skanowanie składników, ale nadal moja klasa adnotacji nie jest wywoływana.