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:
I'm then injecting the feedback message into a WebRequestInterceptor class, which places the message into the request.Code:@Component @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) public class FeedbackMessageImpl implements FeedbackMessage { ... }
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:
My config look like: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( ); }
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.


Reply With Quote