ivanursul

Software engineer

Spring XML and beans inheritance

If you are using XML configuration in your Spring application, you should know about gorgeous feature, that spring has: bean inheritance. What does this mean ? This mean, that you can create an abstract bean with some predefined properties, and later, you will be able to make an inheritance from this bean.

Here is an example

Here you have DefaultDao implementation, which has three fields - persistenceManager, which is the same in every Dao class, queryBuilder for building dynamic queries, which are dynamic field, and entityClass, which is the name of Class, that will be mapped to result from database.

And here we have xml context, that we use without bean inheritance.

After some modification and refactoring we will receive

As you can see, we reduce number of lines in context and now we are instantiating persistenceManager from one place and if we will want to change it, we will do that very quickly.