开发者

Spring autowiring message source vs implementing MessageSourceAware interface?

开发者 https://www.devze.com 2023-01-14 06:56 出处:网络
Which option is better ? I have an interceptor PermissionInterceptor which needs access to message source.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消