-1

I have application where the parent project is updated with the latest Spring library but in the dependent application I do not want to upgrade the spring beans lib version. Since the same dependent application we have to make it compatible with the older parents also which uses the older SF libraries. And also dont want to have multiple dependent application for older and newer parents

But now the problem is that I am getting class cast exception when using this dependent application with newer parent application.

"org.springframework.beans.factory.annotation.Autowire cannot be cast to org.springframework.beans.factory.annotation.Autowire"

Please help me if there is any workaround. Any help would be highly appreciated.

Parent application is using SF 4.1.4RELEASE(older) and 5.2.8RELEASE(upgraded)

Wed May 05 13:37:28 IST 2021 - java.lang.ClassCastException: org.springframework.beans.factory.annotation.Autowire cannot be cast to org.springframework.beans.factory.annotation.Autowire
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:223)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:124)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:318)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:95)
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • Post the error stacktrack – Beshambher Chaukhwan May 05 '21 at 08:37
  • If only one of the parent is upgraded then how about copying the dependent project to a new project with updated version. Its always good to version up things from time to time. – Beshambher Chaukhwan May 05 '21 at 08:39
  • because we have multiple version of parents running at different location, and I want to inject the same dependent application everywhere – Sourav singh May 05 '21 at 08:54
  • Making a copy of that very same dependent application is no different I guess. U just need to copy it and make a new one with updated version. By doing that you are only affecting the parent app and rest others would be using the old dependent app. Changing anything in dependent app will affect other parents also. So its better to make a new one. – Beshambher Chaukhwan May 05 '21 at 09:09
  • is it a parent pom or just a dependency? – Antoniossss May 05 '21 at 09:22
  • Where is your code? what shall we help you with? please post the [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Giorgi Tsiklauri May 05 '21 at 09:26

1 Answers1

1

Exclude the spring-beans dependency from parent project dependency in pom.xml and add the dependency with proper version that you want.

Example: In pom.xml:

<dependeny>
   <artifactId>parent<\artifactId>
   <groupId>parent<groupId>
   <exclusions>
      <exclusion>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
        </dependency>
      </exclusion>
   </exclusions>
</dependency>

and add proper dependency:

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.2.8</version>
    </dependency>
Sourav Sharma
  • 371
  • 1
  • 4
  • 9