Jan 13th, 2010, 11:52 AM
Required annotation and bean inheritance
I have two bean classes Parent and Child, where Child extends Parent. Parent has a field -- parentField -- with a public getter/setter marked as @Required. In the spring config xml a Child bean is defined. The @Required annotation, however, is not enforced if parentField is not set. I have tried defining an abstract bean in the config xml for the Parent class and specifying it as the parent of the Child bean, but this does not work either. @Required enforces things properly for fields defined within the Child class. Does anyone know whether this is supposed to be supported? I can work around it fine by checking in a @PostConstruct method.
Thanks in advance..
Jan 13th, 2010, 06:31 PM
@Required by design is not @Inherited. See more info on annotation inheritance in this blog.
Jan 14th, 2010, 08:29 AM
Thanks a lot for pointing this out. I was now able to achieve the result by creating a custom annotation marked as @Inherited and configuring the RequiredAnnotationBeanPostProcessor as follows:
<bean class="org.springframework.beans.factory.annotatio n.RequiredAnnotationBeanPostProcessor">
<property name="requiredAnnotationType" value="com.test.Mandatory"/>
Tags for this Thread