Dependency Injection in Spring

This was a document I made originally to pin on my bulletin board as a memory aid about how to use the Spring Framework to inject a dependency into another class at run-time.

To use dependency injection:

If you have a bean with a class attribute, Add as a sub-element, add a property tag specifying both name and ref attributes.

<bean class="mypackage.anothersubpackage.MyTestClass">
<property name="manager" ref="registryManager"/>

name = name of the variable to set in the bean class via a call to Set VariableName().
Ex: name=” manager” will result in a call to setManager() in MyTestClass
ref = name of the bean to be injected (this is what will be injected)
Ex: ref=”registryManager” will look for a bean called registryManager to inject

It is frequent that the name and ref properties appear to have the same values because the setter may have a similar (related) name to the bean being set.