So how would we configure our simple voting counter example for the Spring IoC container?
We can use our code exactly as is. All we need to do is inform Spring through an XML configuration file that
the recorder bean is implemented by the LocalVoteRecorder
class. We do this with the following line:
<bean id="recorder" class="com.springindepth.LocalVoteRecorder" />
Then we simply map the recorder bean to the VotingBooth
bean by setter injection
in that beans definition.
<bean id="votingBooth" class="com.springindepth.VotingBooth"> <property name="voteRecorder" ref="recorder"/> </bean>
Spring works its magic to handle class instantiation for you, so your application code never becomes aware of the implementing classes. Now with this configuration and the Spring framework, and through dependency injection, we have finally removed the low-level component dependencies and have achieved true dependency inversion!