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


  1. Replies
    1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

      Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

      Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

      The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training