1.4 Our Example In Spring IoC

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 bean “recorder” is implemented by the LocalVoteRecorder class. We do this with the following line:

                
<bean id="recorder"
      class="com.springindepth.LocalVoteRecorder" />
                
            

Then we simply map this “recorder” object to the VotingBooth object 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!