Results 1 to 4 of 4

Thread: Spring 3.1 Session Scope + WebRequestInterceptor Issue

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Default Spring 3.1 Session Scope + WebRequestInterceptor Issue

    Hi, I am porting my xml configuration to a Java config and I've come across an issue which seems to be Java Config specific.

    I have a feedback message object which has a session scope:
    Code:
    @Component
    @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
    public class FeedbackMessageImpl implements FeedbackMessage {
      ...
    }
    I'm then injecting the feedback message into a WebRequestInterceptor class, which places the message into the request.
    Code:
    public class FeedbackInterceptor implements WebRequestInterceptor {
    
      public static final String FEEDBACK_ATT = "message";
    
      private FeedbackMessage feedbackMessage;
    
      @Autowired
      public void setFeedbackMessage( FeedbackMessage feedbackMessage ) {
        this.feedbackMessage = feedbackMessage;
      }
    
      public void preHandle( WebRequest request ) throws Exception {
        // feedbackMessage is null, why?
        request.setAttribute( FEEDBACK_ATT, feedbackMessage.getMessages( ), RequestAttributes.SCOPE_REQUEST );
        feedbackMessage.clear();
      }
      ...
    }

    Controller class creates messages with no issue:
    Code:
    @Controller
    @RequestMapping("/")
    public class SomeController {
      private FeedbackMessage feedbackMessage;
    
      @Autowired
      public void setFeedbackMessage( FeedbackMessage feedbackMessage ) {
        this.feedbackMessage = feedbackMessage;
      }
    
      @RequestMapping(method = RequestMethod.POST, value = "/update")
      public ModelAndView handleDoSomething( ) {
        // feedbackMessage object exists
        feedbackMessage.addMessageCode( "some.message.keycode" );
        return new ModelAndView(  );
      }
    My config look like:
    Code:
    @Configuration
    @EnableAspectJAutoProxy
    @EnableWebMvc
    @PropertySource("classpath:/example.properties")
    @ComponentScan(basePackages={"com.example"})
    public class ApplicationContextConfig extends WebMvcConfigurerAdapter {
    
      @Override
      public void addInterceptors( InterceptorRegistry registry ) {
        registry.addWebRequestInterceptor( new FeedbackInterceptor( ) );
      }
      ...
    }

    The problem is that the feedbackMessage is null in the FeedbackInterceptor class when the 'preHandle' method is called - I can't work out why it does not work in a Java Config setup, but seems to work fine when using a XML config, anyone have any ideas?

    Ps. I know there is already a message feedback in Spring 3.1, but i'm porting the existing code for now.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Your FeedbackInterceptor isn't a bean and as such not being injected, you are constructing it yourself inside a method. If you want it to be a bean move that to a method annotated with @Bean. So in my book works just as expected.

    Code:
    @Configuration
    @EnableAspectJAutoProxy
    @EnableWebMvc
    @PropertySource("classpath:/example.properties")
    @ComponentScan(basePackages={"com.example"})
    public class ApplicationContextConfig extends WebMvcConfigurerAdapter {
    
      @Override
      public void addInterceptors( InterceptorRegistry registry ) {
        registry.addWebRequestInterceptor( feedbackInterceptor() );
      }
    
      @Bean
      public FeedbackInterceptor feedbackInterceptor() {
        return new FeedbackInterceptor();
      }
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Default

    That worked, it seems abit obvious thinking about it now. Also, I needed to add the below, which I thought was no longer needed in Spring 3.1.

    Code:
     
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
      </listener>
    Thanks

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    If the RequestContextListener is needed depends on where/how you want to use your scoped beans.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •