Spring framework is widely used as a dependency injection container, and that’s for good reasons. First of all, it facilitates integration testing and it gives us the power of customizing bean creation and initialization (e.g. @Autowired for List types).
But there is also a very useful feature, that might get overlooked and therefore let’s discuss about bean aliasing.
Bean aliasing allows us to override already configured beans and to substitute them with a different object definition. This is most useful when the bean definitions are inherited from an external resource, which is out of our control.
In the following example I will show you how bean aliasing works. Let’s start with the following bean definition, coming from the src/main/resources/spring/applicationContext-tx.xml configuration file.
This context file contains many transaction/JPA related features we would like to use in our Integration Tests.
The dataSource bean definition expects a XA Datasource, but since…