首页 > 其他分享 >spring注入bean错误-Bean named 'abc' is expected to be of type 'AAA' but was actually

spring注入bean错误-Bean named 'abc' is expected to be of type 'AAA' but was actually

时间:2023-08-16 19:22:46浏览次数:40  
标签:named abc java support factory springframework beans org type

先看如下两个注入到spring容器中的bean, 一个是 UserNewManager, 一个是 UserManager。

@Service
public class UserNewManager {
    public void doSomething() {

    }

}
@Service
public class UserManager {
    ...
}

再看下面的testcase,利用@Resource注解来注入bean。

@SpringBootTest
@RunWith(SpringRunner.class)
public class ResourceTest {

    @Resource
    private UserNewManager userManager;

    @Test
    public void test() {
        userManager.doSomething();
    }
}

在运行test方法时,会抛异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springtrans.resourcetest.ResourceTest': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'userManager' is expected to be of type 'springtrans.resourcetest.UserNewManager' but was
actually of type 'springtrans.resourcetest.UserManager'

 

原因:@Resource注解有两个重要的属性:name和type。在一个使用@Resource来注入bean的声明语句中,@Resource优先是按name来解析bean的,当无法解析到bean时会再按照声明type来解析bean,解析完成后注入到对象上。在此testcase案例中,@Resource先按照“userManager”定位到了UserManager这个type,而这个UserManager与实际期望的类型 UserNewManager 产生冲突。就报这个 BeanNotOfRequiredTypeException 异常。 另一种情况,假如容器里没有UserManager这个bean的话,@Resource按照“userManager”就无法解析到type,它会继续按照声明的UserNewManager这个type来解析bean。

 

 

我们的项目分为三层,service层→manager层→mapper层,上层依赖下层,相同层级之间一般不相互依赖。其中manager层有一个DataCacheManager,用来做数据缓存。我在一次代码重构中,为了规避未来可能产生的循环引用,我将 DataCacheManager依赖的其他manager改为依赖对应mapper。 例如:我将  @Resource private WorkTypeManager workTypeManager;  改为了  @Resource private WorkTypeMapper workTypeManager;  。结果,在启动springboot服务时,就出现了这个 BeanNotOfRequiredTypeException:Bean named 'workTypeManager' is expected to be of type 'com.xxx.mapper.WorkTypeMapper'but was actually of type 'com.xxx.manager.workTypeManager$$EnhancerBySpringCGLIB$$30dd8c09'。 详细堆栈如下:

2023-08-03 22:30:48.588 [TID:N/A] [] [main] INFO  o.a.d.s.b.c.e.AwaitingNonWebApplicationListener:162 -  [Dubbo] Current Spring Boot Application is about to shutdown...
2023-08-03 22:30:48.619 [TID:N/A] [] [main] ERROR org.springframework.boot.SpringApplication:858 - Application run failedorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskApplyDesignImportUserService': Unsatisfied dependency expressed through field'taskApplyManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskApplyManager': Unsatisfied dependency expressed through field 'dataCacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'dataCacheManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'workTypeServiceManager' is expected to be of type 'com.xxx.mapper.WorkTypeMapper'but was actually of type 'com.xxx.manager.WorkTypeServiceManager$$EnhancerBySpringCGLIB$$30dd8c09'
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
        at com.xxx.ServerApplication.main(ServerApplication.java:41)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskApplyManager': Unsatisfied dependencyexpressed through field 'dataCacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataCacheManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean named 'workTypeServiceManager' is expected to be of type 'com.xxx.mapper.WorkTypeMapper' but was actually of type 'com.xxx.manager.WorkTypeServiceManager$$EnhancerBySpringCGLIB$$30dd8c09'
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
        ... 27 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataCacheManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'workTypeServiceManager' is expected to be of type 'com.xxx.mapper.WorkTypeMapper' but was actually of type 'com.xxx.manager.WorkTypeServiceManager$$EnhancerBySpringCGLIB$$30dd8c09'
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
        ... 40 common frames omitted
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'workTypeServiceManager' is expected to be of type 'com.xxx.mapper.WorkTypeMapper' but was actually of type 'com.xxx.manager.WorkTypeServiceManager$$EnhancerBySpringCGLIB$$30dd8c09'
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:392)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:452)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:526)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:636)
        at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
        ... 51 common frames omitted
View Code

 

是为记。

 

标签:named,abc,java,support,factory,springframework,beans,org,type
From: https://www.cnblogs.com/buguge/p/17635999.html

相关文章

  • AtCoder-ABC-267 C - Index × A(Continuous ver
    C-Index×A(Continuousver.)题目大意:给定n个数(\(a_1,a_2...a_n\)),从中选连续m个数,这m个数的和为:\(\sum_{i=1}^mi*b_i\)求最大的和为多少。\(1<=m<=n<=2*10^5\)\(-2*10^5<=a_i<=2*10^5\)解题思路首先m个数为一组,那么最多有n-m+1组,这个数量是可以被遍历的,但是......
  • AtCoder-ABC-309 C - Medicine
    C-Medicine题目大意:给n种药,第i种药吃\(a_i\)天,每天\(b_i\)粒。问最早在第几天,当天要吃的药≤K。\(1<=n<=3*10^5\)\(0<=k<=10^9\)\(1<=a_i,b_i<=10^9\)解题思路首先了解了n种药,每次都是从第一天开始,持续\(a_i\)天,所以我当时直接想到用差分来做,数组初始全为......
  • typeScript学习-interface和type 区别
    typeScript学习interface(接口)和type区别type和接口类似,都用来定义类型,但type和interface区别如下:区别1:定义类型范围不同interface只能定义对象类型或接口当名字的函数类型。type可以定义任何类型,包括基础类型、联合类型、交叉类型,元组。//type定义基础类型typ......
  • 报错TypeError: Cannot read properties of null (reading 'length')
    可能是某个数组属性不存在,但是判断了他的长度,比如下方代码<templatev-if="arr.length"><divv-for="(item,idx)inarr":key="idx">{{item}}</div></template>解决方法  v-if="arr......
  • 【230816-8】▲ABC中,AB=4,BC=2,∠A=α=∠B/2,求:AC=?
    ......
  • Grafana日志转换-Convert field type & Concatenate fields & Organize fields
    Grafana日志转换-Convertfieldtype&Concatenatefields&Organizefields转换函数根据自己的使用情况,记录使用到的转换函数,这次涉及到的转换函数如下Convertfieldtype功能:转换字段的类型Concatenatefields功能:将所有字段合并为一个数据集合。如果长度不相同,值将附加......
  • NetSuite: Get specific Custom Record Types and related sub Custom Fields
    背景以前当使用search.create({})来获取数据时,我们需要制定特定的数据返回列;例如:search.createColumn(options)而query可以使用SELECT*FROM来动态返回所有的数据列(这在有的时候是一个优点),那么如何让search也动态返回所有的数据列呢?vararrColFlds=query.runSuiteQL({......
  • ABC314 E和CF892 Div2D-E
    ABC314EE-Roulettes(atcoder.jp)大致意思是给你n个轮盘,第i个轮盘等概率的p[i]个点数,玩一次c[i]价钱,问要达到m点的最小期望花费是多少,每次可以任意选一个。乍一看很像背包,偏了方向,所以当时没有做出来。也考虑过其它的DP,关键是0怎么处理没搞明白所以赛后看他人的代码和题解......
  • [ABC134F] Permutation Oddness 题解
    题面定义一个\(1\simn\)的排列\(p\)的「怪异度」为\[\sum_{i=1}^n\left\lvertp_i-i\right\rvert\]求「怪异度」为\(k\)的\(1\simn\)的排列数,答案对\(10^9+7\)取模。题解考虑转化计算怪异度的过程,我们将值\(p_i\)排列在左侧,将下标\(i\)排列在右侧,构成一个......
  • [ABC134F] Permutation Oddness
    题目大意定义一个\(1\simn\)的排列\(p\)的「怪异度」为\[\sum_{i=1}^n|p_i-i|\]求「怪异度」为\(m\)的\(1\simn\)的排列数,答案对\(10^9+7\)取模。思路考虑把\(p_i\)和\(i\)看作小球与盒子,方便题意理解。考虑球与盒子的匹配。假设球在左侧,盒子在右侧,他们......