answered
2014-04-01 19:09:53 +0800
Crow 3 ● 2 Reading a bit about dependency injection on Stack Overflow (I can't post links so look for 'what is dependency injection?' and 'Dependency Inject vs factory pattern') I get several pieces of information about DI that makes me question the assertion that this is dependency injection.
Attributes of dependency injection include:
- An object is not responsible for creating objects
- An object is not responsible for obtaining the objects it needs
- An object does not know how it get's it's required objects.
While the first criteria is true I don't see that the last two would be true if I used SpringUtil.getBean()
. In this case, the class using it is responsible for obtaining it's dependencies and has to get them itself.
It appears the Zk Framework does assist to provide that dependency and keeps a copy of the bean, however if I use the SpringUtil.getBean()
method my class still has the responsibility to obtain the bean.
It seems to me with that, the Hollywood principle demonstrates how this approach does break dependency injection. For those who don't know that principle is 'Don't call us, we'll call you'. In my interpretation that mean don't be responsible for calling to get the objects you need - have them given to you.
One further problem with this is that I find the class is not clear about it's required dependencies. I don't think it's clear requirement but being able to create or obtain objects inside a method of an object means the class isn't clear with it's outside world about what dependencies it requires