I was thinking about why Spring doesn't allow you to inject static fields. Injecting static fields looks like a wrong usage of an OOP pattern (dependency injection), also static fields belong to a Class, not to an Object;
Can someone explain technically why Sping doesn't allow (or is not able) to autowire static fields?
Kirk James wrote:Injecting static fields looks like a wrong usage of an OOP pattern (dependency injection), also static fields belong to a Class, not to an Object;
Those are both good reasons not to inject static fields, so I think you have the answer well-covered.
Kirk James wrote:Injecting static fields looks like a wrong usage of an OOP pattern (dependency injection), also static fields belong to a Class, not to an Object;
Those are both good reasons not to inject static fields, so I think you have the answer well-covered.
On a techical side, what could be the reason of that behaviour?
Kirk James wrote:I was just talking about the Spring IOC container that manages the bean instances and that cannot work with static elements.
It looks like you've assumed that "cannot" is the right word to use there. It's possible that DI can work with static members but that the designers of Spring have decided not to do that. After all there are serious architectural reasons not to do it, as you already noted.
They weren't very bright, but they were very, very big. Ad contrast:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop