This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

final properties in abstract bean definition

Sep 28th, 2009, 05:49 PM

I'm looking for a way to ensure that children of an abstract bean do not override certain properties. To give a slightly-contrived but easy-to-understand example: assume I have a set of beans representing different kinds of lifeforms, all represented as instances of the Java class Lifeform. Lifeforms have a "kingdom" property representing their biological kingdom. There is an abstract bean with id="Animal" that sets kingdom=Animalia. I want to ensure that any bean with parent="Animal" does not override (accidentally or intentionally) this property, so that all animals have the correct kingdom.

One way I have found to do this is to create a Java class Animal whose constructor calls super.setKingdom("Animalia"), and overrides setKingdom to throw an exception. However I would prefer to not have "Animalia" hard-coded in the implementation, but set within the spring config instead.

Spring supposes that child bean can override every parent bean property, so, there is no out-of-the-box solution.

Another point is that such a check doesn't seem really necessary, i.e. if you maintain spring config it's up to you to not overwrite parent properties. Even if you accidentally overwrite a property you can fix that easily as you are a config maintainer.

If you still want to introduce that rule you can create custom BeanFactoryPostProcessor and implement that logic there.