Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3: disable SpEL evaluation of a bean property value?

We're in the process of updating our apps from Spring 2.5 to 3.0 and we've hit a problem with the new SpEL evaluation of bean properties.

We've been using an in-house templating syntax in one module which unfortunately uses the same "#{xyz}" markup as SpEL. We have a few beans which take string's containing these expressions as properties but spring assumes they are SpEL expressions and throws a SpelEvaluationException when it tries to instantiate the bean.

e.g.

<bean id="templatingEngine" class="com.foo.TemplatingEngine">
   <property name="barTemplate" value="user=#{uid}&country=#{cty}"/>
</bean>

Is it possible to disable SpEL evaluation, ideally per-bean, but alternatively for the whole application context?

Alternatively is there a way to escape the values?

Thanks, Stephen

like image 952
Stephen Avatar asked Dec 16 '10 17:12

Stephen


1 Answers

Completely disable SpEL evaluation by calling the bean factory setBeanExpressionResolver method passing in null. You can define a BeanFactoryPostProcessor to do this.

public class DisableSpel implements BeanFactoryPostProcessor {
    public void postProcessBeanFactory(
        ConfigurableListableBeanFactory beanFactory)
        throws BeansException
    {
        beanFactory.setBeanExpressionResolver(null);
    }
}

Then define this bean in the application context.

<bean class="com.example.spel.DisableSpel"/>
like image 72
Chin Huang Avatar answered Sep 23 '22 02:09

Chin Huang