Skip to content

Cannot reference FactoryBean in a SpEL expression [SPR-9511] #14145

Closed
@spring-projects-issues

Description

@spring-projects-issues

matthew inger opened SPR-9511 and commented

Status Quo

Currently, if you try to reference a FactoryBean in a SpEL expression using standard syntax, you get an error as follows.

Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is java.lang.IllegalStateException: Cannot handle (38) '&'
	at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142)
	at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1299)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:224)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:311)
	... 45 more
Caused by: java.lang.IllegalStateException: Cannot handle (38) '&'
	at org.springframework.expression.spel.standard.Tokenizer.process(Tokenizer.java:193)
	at org.springframework.expression.spel.standard.Tokenizer.<init>(Tokenizer.java:47)
	at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:110)
	at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:56)
	at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:1)
	at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:128)
	at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:74)
	at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:64)
	at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:119)
	... 48 more

Work-Around

I can work around this by injecting the AnnotationSessionFactoryBean into my initializer, but that will break when I move to Hibernate 4. Alternatively, I could use a BeanReferenceFactoryBean and get the BeanFactory to retrieve a named bean and then use SpEL from there. Either seems like a hack, and SpEL should support this IMHO.

Example Code

Here's the relevant XML configuration:

    <bean id="entityCallbackHandler" class="org.hibernate.ejb.event.EntityCallbackHandler "/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
     ...
    </bean>

    <bean id="entityCallbackHandlerInitializer"
          class="hibernate.utilities.EntityCallbackHandlerInitializer">
    	<property name="entityCallbackHandler" ref="entityCallbackHandler" />
    	<property name="configuration" value="#{&amp;sessionFactory.configuration}" />
    </bean>

And the java code for the initializer class:

public class EntityCallbackHandlerInitializer {

  private Configuration configuration;

  private EntityCallbackHandler entityCallbackHandler;
  
  public void setConfiguration(Configuration configuration) {
      this.configuration = configuration;
  }

  public void setEntityCallbackHandler(EntityCallbackHandler entityCallbackHandler) {
      this.entityCallbackHandler = entityCallbackHandler;
  }

  @PostConstruct
  public void init() throws ClassNotFoundException {
      final ReflectionManager reflectionManager = configuration.getReflectionManager();
      final Iterator<PersistentClass> classMappings = configuration.getClassMappings();
      while (classMappings.hasNext()) {
          entityCallbackHandler.add(
                  reflectionManager.classForName(classMappings.next().getClassName(), this.getClass()),
                  reflectionManager);
      }
  }
}

Issue Links:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions