简体   繁体   English

获取 @Autowired 的 ClassCastException

[英]Getting ClassCastException for @Autowired

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.我有使用最新的 Spring 库更新父项目的应用程序,但在依赖应用程序中我不想升级 spring bean 库版本。 Since the same dependent application we have to make it compatible with the older parents also which uses the older SF libraries.由于相同的依赖应用程序,我们必须使其与使用旧 SF 库的旧父母兼容。 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.但现在的问题是,当将此从属应用程序与较新的父应用程序一起使用时,我得到了 class 强制转换异常。

"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)父应用程序正在使用 SF 4.1.4RELEASE(旧)和 5.2.8RELEASE(升级)

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)

Exclude the spring-beans dependency from parent project dependency in pom.xml and add the dependency with proper version that you want.从 pom.xml 中的父项目依赖项中排除 spring-beans 依赖项,并使用您想要的正确版本添加依赖项。

Example: In pom.xml:示例:在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM