tags:

views:

28

answers:

1

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?

+1  A: 

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 @Autowired then 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.

public class PermissionInterceptor extends HandlerInterceptorAdapter {

   @Autowired
   private MessageSource messageSource;

   ...
}

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.

Mike Q
+1 `MessageSourceAware` was the only option pre-Spring 2.5, but `@Autowired` makes it a bit nicer.
skaffman