MVEL version: | 2.1.0.drool8 + |
Spring version: | 3.1 + |
Issue: | Error invoking getBean method in mvel code |
Description
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
Causes:
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)
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.
Spring Training Institutes in Chennai Java EE Training Institutes in Chennai Java EE Training Institutes in Chennai Java Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai
ReplyDelete