Which option is better ?
I have an interceptor PermissionInterceptor which needs access to message source.
As far as I know it can be done by autowiring message source or implementing MessageSourceAware interface as follows.
public class PermissionInterceptor extends HandlerInterceptorAdapter {
private MessageSource messageSource;
@Autowired
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
}
or
public class PermissionInterceptor extends HandlerInterceptorAdapter implements MessageSourceAware {
private MessageSource messageSource;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
}
Which option is better? Any pros and cons?
There’s no real big pros/cons. Generally it is just a matter of preference of the coder. I would say that if you are going to use
@Autowiredthen put the annotation of the field and drop the method. This makes it a bit more concise, which for me is the benefit of the annotations.It may also depend whether you use the annotations rather than XML bindings in the rest of your app config. If you don’t use the annotations elsewhere then I would probably avoid doing it in this case for consistencies sake.