It actually depends on the role of the component you are referring to: Is this feature:
- An internal tooling: you can use static (you wouldn't wrap
Math.abs
or String.trim
in a bean)
- Or a module of the project: design it to be a bean/module-class (a DAO class is best modular to be able to change/mock it easily)
Globally, you should decide w.r.t your project design what are beans and what are not. I think many dev put too much stuff inside bean by default and forget that every bean is an public api that will be more difficult to maintain when refactoring (i.e. restrained visibility is a good thing).
In general, there are already several answers describing the advantages of using spring beans, so I won't develop on that. And also note that you don't need spring to use bean/module design. Then here are the main reasons not to use it:
- type-safety: Spring bean are connected "only" at runtime. Not using it, you (can) get much more guaranties at compile time
- It can be easier to track your code as there is no indirection due to IoC
- You don't need the additional spring dependency/ies which get quite heavy
Obviously, the (3) is correct only if you don't use spring at all in your project/lib.
Also, The (1) and (2) really depend on how you code. And the most important is to have and maintain a clean, readable code. Spring provides a framework that forces you to follow some standard that many people like. I personally don't because of (1) and (2), but I have seen that in heterogeneous dev teams it is better to use it than nothing. So, if not using spring, you have to follow some strong coding guidelines.
Foo
does not have any state and contains onlystatic
methods, it's more of a utility class. No point making this aSpring
bean since you can't really swap implementations if aclass
only hasstatic
methods. – Moffett