Utility class in Spring application - should I use static methods or not?

前端 未结 3 1410
北海茫月
北海茫月 2021-01-30 12:23

Let\'s say I have a utility class DateUtil (see below). To use this method a caller method uses DateUtils.getDateAsString(aDate). Would it be better to remove the static modifie

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 13:08

    It would be better to declare it as a Spring bean because the life cycle of it is then managed by Spring, and you can eventually inject dependencies, pool the object, as well as test it in a proper way, not to talk that you could use it as a regular object and pass it as parameter, redefine the method in subclasses... etc.

    In short, yes it would be a better design in most cases. Nevertheless, in a case as simple as the exposed, it doesn't do a great difference.

提交回复
热议问题