Tuesday, May 29, 2012

MVEL 2.1.0.drool8 and up is not compatible with Spring 3.1

MVEL version:2.1.0.drool8 +
Spring version:3.1 +
Issue:Error invoking getBean method in mvel code


If you are using mvel version 2.1.0.drool8 and up and Spring 3.1 and up, you are likely getting the following error message when you invoke the getBean(String) method in your mvel code.

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
 Can only specify arguments for the getBean method when referring to a prototype bean definition
 at org.springframework.beans.factory.support.AbstractBeanFactory.checkMergedBeanDefinition(AbstractBeanFactory.java:1215) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
 ... 87 common frames omitted


After some digging into the mvel source code, it shows that ReflectiveAccessorOptimizer class is calling ParseTools.getBestCandidate method to find the matching method. The way the method finds the matching candidate is by assigning a score to each candidate. The method that score highest is the best candidate. If there there are more than one methods score the same, the first one wins.
Here is the problem: Spring's ApplicationContext class has multiple getBean methods and two of them score the same:
getBean(String, Object[]).
If you invoke getBean("myBeanId"), you will want the first method to be invoked. However, in mvel 2.1.0.drool8 and up, the ApplicationContext instance mvel retrieved is XMLApplicationContext, in which getBean(String, Object[]) is before getBean(String) in the list. So the best candidate becomes getBean(String, Object[ ]) and so on the Spring will throw the exception above.


Solution 1

The easy fix is to use mvel version 2.1.0.drool7

Solution 2

In order to use use the latest mvel library, create a wrapper class around the applicationContext, i.e. ApplicationContextWrapper. Then implement a helper method called getBean(String) that calls getBean(String) method in ApplicationContext class. Then use this helper method in your mvel code. e.g. applicationContextWrapper.getBean("myBeanId"); This way, there is only one method named getBean in the new wrapper class.

Solution 3

Wait for MVEL team to come up with a resolution to this issue. Maybe a better scoring system. getBean(String) and getBean(String, Object[]) should score differently. [Edit] I created a JIRA entry for MVEL team: MVEL-276