Salman Kazmi Salman Kazmi - 2 months ago 45
Java Question

NoSuchMethodError on compiling Spring Application with FileSystemXmlApplicationContext

When running a very basic Spring app and creating a Bean Factory with FileSystemXmlApplicationContext, I encounter a NoSuchMethodError.

The beans.xml file is at the root of the app with pom.xml and looks like this:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean id="person" class="com.xyz.practice.Person">

</bean>

</beans>


The Person class looks like:

package com.xyz.practice;

public class Person {
public void speak(){
System.out.println("Hello I'm a Person");
}
}


And the main class App.java looks like the following:

package com.xyz.practice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
Person person = (Person) context.getBean("person");
person.speak();
}

}


When running the app, the following exception is thrown:

INFO: Loading XML bean definitions from file [/home/salmank/Documents/springWithAbc/beans.xml]
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableListableBeanFactory.clearMetadataCache()V
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:185)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
at com.xyz.practice.App.main(App.java:9)

Answer

You have incompatible jar files in your classpath. Check the Version of spring-context-support and spring-beans The missing method was introduced in spring 4.2:

void clearMetadataCache()

Clear the merged bean definition cache, removing entries for beans which are not considered eligible for full metadata caching yet. Typically triggered after changes to the original bean definitions, e.g. after applying a BeanFactoryPostProcessor. Note that metadata for beans which have already been created at this point will be kept around.

Since:4.2

See Also:getBeanDefinition(java.lang.String), ConfigurableBeanFactory.getMergedBeanDefinition(java.lang.String)

Check if Version of spring-context-support is the same then spring-beans

By the way: remove the Version at the end of the xsd Location:

http://www.springframework.org/schema/beans/spring-beans.xsd">