Understanding inversion of control
IoC(Inversion of control)是Spring的心臟,聽著感覺它比較晦澀。其實它遠不像聽著的那樣難懂。事實上,在你的工程中引入IoC之后,你會發現你的代碼會變得非常簡樸,更易于理解和測試。
但是,究竟什么是IoC呢?
Injecting dependencies
In an article written in early 2004, Martin Fowler asked what aspect of control is being inverted. He concluded that it is the acquisition of dependent objects that is being inverted. Based on that revelation, he coined a better name for inversion of control: dependency injection.
Traditionally, each object is responsible for obtaining its own references to the objects it collaborates with (its dependencies). As you'll see, this can lead to highly coupled and hard-to-test code.Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system.That is, dependencies
are injected into objects. So, IoC means an inversion of responsibility with regard
to how an object obtains references to collaborating objects.
IoC in action