Announcement Announcement Module
Collapse
No announcement yet.
final properties in abstract bean definition Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • final properties in abstract bean definition

    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.

    How would you accomplish this? Thanks for your help.

  • #2
    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.

    Comment

    Working...
    X