This article is no longer available.
Return to the java.net home page
by Anonymous - 2006-05-09 11:54
by Anonymous - 2006-05-09 14:46
The Inversion of Control (IoC) frameworks (such as Spring) essentially provide generic class factories and then use some mechanism (typically XML files) to "wire" object dependencies in order to remove such "wiring" from your code.
The framework resolves the configured dependencies at runtime for you, rather than hardcoding them yourself.
The technique is called "injection" and, yes, it's simply setting a property in a class.
To leverage the provided example, in this case the JDBC methods call a "getConnection" routine in order to acquire a JDBC connection. Using an IoC framework, rather than using getConnection, you'd simply have a Connection member variable. When you went to get a new instance of your class, rather than calling "new YourClass", you'd ask the IoC framework for it instead.
The IoC framework would see that you have a Connection (because you configured it that way), and when you got your object from the framework, the Connection member variable would come "pre-populated". So the class doesn't need to know how to get a Connection, as one is "injected" in to it when the class is created.
The competing techniques are "contructor injection" and "setter injection", meaning that the properties of an object are set either via the constructor or simply a setter.
The whole goal around IoC frameworks is to keep the objects as simple as possible by pulling the dependencies out of them. By relying on a factory to create your objects, the factory assumes the burden of knowing how to create the different resources (like Connections). Combine this with using Interfaces instead of Objects, and it makes unit testing much easier.
In this case, you could reconfigure the IoC framework to provide your class with a "fake Connection", used only for testing. If your class has a "hard coded" getConnection method, then you'd have to change the class to get another kind of Connection. Or, you wouldn't even have to use the framework at all. Simply do "new YourClass", call it's "setConnection" method, and then run the logic. The class itself stays the same for both the application and the unit test.
That's just a simple example of the basics of an IoC framework. The modern ones take the simple concept and add on to it in many ways.
by Anonymous - 2006-05-11 10:18
by Anonymous - 2006-05-15 14:59
by Anonymous - 2006-05-18 10:34
by Anonymous - 2006-05-26 06:57
by Anonymous - 2006-05-16 14:47
Your use of this web site or any of its content or software indicates your agreement to be bound by these Terms of Participation.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.