0

I'm using NamespaceHandler and BeanDefinitionParser based on reference and I'd like to use Spel with bean references. The problem is, that I cannot reach app context in NamespaceHandler or BeanDefinitionParser due to the lifecycle phase we're in, so that when my BeanDefinitionParser interprets Spel expressions it doesn't have BeanFactory available for evaluating spel with bean references. Any idea how to deal with this ? If the SpEL was placed on bean properties I could leave it be and have spring interpret it, but unfortunately the SpELs are in the XML that is to be unmarshalled to an object model that is to be used to construct the bean property.

EDITED: based on Gary Russell's advice

<bean id="securityDecriptor" class="com.fg.edee.integration.descriptor.security.SecurityDescriptorImpl">
    <property name="securityModuleName" value="security"/>
    <property name="securityModuleDescription" value="security"/>
</bean>

<bean id="moduleDescription" class="com.fg.edee.integration.descriptor.schema.ModuleDescription">
    <property name="treeDescriptor">
        <mtd:treeDescriptor>
            <mtd:security rights="W" moduleId="#{ securityDecriptor.securityModuleId }" moduleName="#{ securityDecriptor.securityModuleName }">
                <mtd:securityIdentifier identifierId="users" identifierName="users"/>
            </mtd:security>
        </mtd:treeDescriptor>
     </property>
</bean>
lisak
  • 20,109
  • 39
  • 146
  • 236

1 Answers1

0

You cannot evaluate them in the parser; it's too early in the context's lifecycle. There are several approaches - for example, have the parser build a definition for a factory bean that creates the final object during context initialization.

Gary Russell
  • 131,626
  • 14
  • 99
  • 125
  • That's the whole point of BeanDefinitionParser, isn't it ? I create a bean definition from my "custom" xml, but I need to use spel with bean references in there. I don't really see your point. Although I do understand that in the "bean definition building" lifecycle phase the referenced bean is not available for me to access it... – lisak Aug 21 '13 at 13:49
  • Right, you are creating bean `definitions`, not beans; at this point there are no other beans to reference, only bean definitions. Actual references are only created during the bean instantiation phase, which does not begin until ALL definitions have been created. AT this point, all you can do is add the `SpEL` expression to your bean definition, it will be evaluated lated. – Gary Russell Aug 21 '13 at 13:51
  • Well the problem is that if I don't evaluate it by myself in BeanDefinitionParser, it is not evaluated later on because the SpELs are not placed on the bean properties but on my object model (that is constructed from the xml) that is finally assigned to the bean property :-) As you can see on the snippet, I unmarshall treeDescriptor element to an object and assign it to the constructor of property treeDescriptor. – lisak Aug 21 '13 at 13:59
  • You simply can't do it that way `securityDescriptor` doesn't exist yet; use a factory bean. – Gary Russell Aug 21 '13 at 14:48