Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethodError on compiling Spring Application with FileSystemXmlApplicationContext

Tags:

java

spring

maven

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)
like image 291
Salman Kazmi Avatar asked Sep 27 '16 05:09

Salman Kazmi


3 Answers

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">
like image 101
Jens Avatar answered Nov 12 '22 07:11

Jens


You are mixing different versions together in pom.xml (spring-core.jar and spring-beans.jar). Please correct it as following.

Before:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.1.1.RELEASE</version>
</dependency>   

After:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>
like image 40
S.Basnagoda Avatar answered Nov 12 '22 08:11

S.Basnagoda


This is due to the version mismatch between the Spring liberary.

void clearMetadataCache() method is added into ConfigurableListableBeanFactory since 4.2.0 version release. So if you are using spring-beans library version less than 4.2.0 versions, you will probably get this Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableListableBeanFactory.clearMetadataCache()V kind of exception.

So, try adding proper version jar into lib.

Added Methods ::
    spring-beans-4.2.0.RELEASE.jar, 
    ConfigurableListableBeanFactory.class
    package org.springframework.beans.factory.config
    ConfigurableListableBeanFactory.clearMetadataCache ( ) [abstract]  :  void 

For more details you may refer this URL too: https://abi-laboratory.pro/java/tracker/compat_report/spring-framework/4.1.9/4.2.0/e1bb7/src_compat_report.html#Added

like image 36
shripad vade Avatar answered Nov 12 '22 08:11

shripad vade