How to prevent @ModelAttribute for instantiating a calendar in a method

I have a method in the controller:

public FormValidationResult submitFormAndSendEmail(@Valid ContactForm form, BindingResult result,
  @HttpSessionParam(value = "lastTimeContactFormSent", required = false) Calendar lastTimeContactFormSent)

As you can see, I created the @HttpSessionParam annotation, this will take a variable from HttpSession and put it in the specified method parameter.

However...

Before resolving the argument, I get an instance of InstantiationExceptionConstructorAccessorImpl because the calendar cannot be created by the default constructor.

Using the given stack, I can see that HandlerMethodInvoker inside "resolveModelAttribute" causes an instance to be created.

How can I prevent this? I don't want to instantiate, I want to use my own WebArgumentResolver to populate the method parameter.

Any clues?

Additional Information: Stacktrace by Spring (3.0.4):

    java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:772)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)

, webargumentresolver.

(xml) :

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="customArgumentResolver" ref="sessionParamResolver"/>
</bean>  

<!--  annotation to resolve httpSession attributes -->
<bean id="sessionParamResolver" class="nl.usgpeople.vakgilde.spring.mvc.extensions.SessionParamArgumentResolver"/>
+3
2

, axtavt , . WebArgument . xml . ?

-context.xml mvc-context.xml( import). mvc-context.xml bean ..

bean mvc-context.xml, , Spring "" bean .

, , , sessionParamResolver bean , . bean .

Spring , 15.12. Spring MVC, , ANTONTOMMODHANDERAdapter. , , customArgumentResolvers.

+1

, resolver . , :

  • AnnotationHandlerMethodAdapter
  • ,
+1

Source: https://habr.com/ru/post/1791448/


All Articles