Error creating bean with name 'userService' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through

本文解析了Spring框架中出现的依赖注入错误,特别是在使用byType自动装配方式时遇到的 UnsatisfiedDependencyException 异常。该异常发生在多个相同类型的Bean存在但仅期望一个的情况下。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through bean property 'userDAO': : No unique bean of type [inspur.democreen.dao.UserDAO] is defined: expected single matching bean but found 2: [userDAO, userDAO2]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [inspur.democreen.dao.UserDAO] is defined: expected single matching bean but found 2: [userDAO, userDAO2]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
 at java.security.AccessController.doPrivileged(Native Method)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
 at inspur.democreen.service.UserServiceTest.testAdd(UserServiceTest.java:13)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [inspur.democreen.dao.UserDAO] is defined: expected single matching bean but found 2: [userDAO, userDAO2]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1076)
 ... 32 more

 

 

在测试用例中,打印出 主动注入的 类中属性值。

spring配置文件如下

<bean name="userDAO" class="inspur.democreen.dao.impl.UserDAOImpl">
  <property name="daoId" value="1"></property>
 </bean>
 
 <bean name="userDAO2" class="inspur.democreen.dao.impl.UserDAOImpl">
  <property name="daoId" value="2"></property>
 </bean>
 <bean id="userService" class="inspur.democreen.service.UserService"
  scope="prototype" autowire="byType">
 </bean>

 

 

由于

autowire="byType"

那么userDervice中的type(inspur.democreen.dao.impl.UserDAOImpl)有两个(userDAO,userDAO2)所以报出异常

 

注释一个,byType自动注入 测试用例通过。

 

释:这种自动注入用得较少,一般都不想用这种方式注入。

 

 

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ODataConnectorImpl': Unsatisfied dependency expressed through field 'messages'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'weibaobiaoMessages': Unsatisfied dependency expressed through field 'securityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amsSecurityService': Unsatisfied dependency expressed through field 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in class path resource [security-service.xml]: Cannot resolve reference to bean 'amsAuthorizationRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amsAuthorizationRealm': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'customerService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerServiceImpl': Unsatisfied dependency expressed through field 'metadataService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultMetadataServiceImpl': Unsatisfied dependency expressed through field 'zxtbTask'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'zxtbTask': Unsatisfied dependency expressed through field 'redisDistributeLock'; nested exception is org.springframework.beans.f
最新发布
07-09
2025-03-27 15:47:51.709 ERROR 24032 - [restartedMain] [o.s.b.web.embedded.tomcat.TomcatStarter,onStartup,61] : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'registration' defined in class path resource [com/cloudweb/oa/security/BeanConfig.class]: Unsatisfied dependency expressed through method 'registration' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jwtFilter': Unsatisfied dependency expressed through field 'jwtUtil'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jwtUtil': Unsatisfied dependency expressed through field 'userCache'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userCache': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userGroupService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'groupServiceImpl': Unsatisfied dependency expressed through field 'departmentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'departmentServiceImpl': Unsatisfied dependency expressed through field 'deptUserService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptUserServiceImpl': Unsatisfied dependency expressed through field 'userSetupService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userSetupServiceImpl': Unsatisfied dependency expressed through field 'roleService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'roleServiceImpl': Unsatisfied dependency expressed through field 'rolePrivService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rolePrivServiceImpl': Unsatisfied dependency expressed through field 'userOfRoleService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userOfRoleServiceImpl': Unsatisfied dependency expressed through field 'syncUtil'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cloudweb.oa.api.ISyncUtil' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
03-28
无法解析的成员访问表达式[SF_GET_OCT_LENGTH]> 2025-03-10 14:30:34,146 ERROR [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] - <Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: jpaServiceRegistryContext] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.DataException: Error accessing tables metadata> 2025-03-10 14:30:34,149 ERROR [org.springframework.boot.web.embedded.tomcat.TomcatStarter] - <Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'casCorsFilter' defined in class path resource [org/apereo/cas/config/CasFiltersConfiguration$CasFiltersCorsConfiguration.class]: Unsatisfied dependency expressed through method 'casCorsFilter' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'corsHttpWebRequestConfigurationSource' defined in class path resource [org/apereo/cas/config/CasFiltersConfiguration$CasFiltersCorsConfiguration.class]: Unsatisfied dependency expressed through method 'corsHttpWebRequestConfigurationSource' parameter 3; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'servicesManager' defined in class path resource [org/apereo/cas/config/CasCoreServicesConfiguration$CasCoreServicesManagerConfiguration.class]: Unsatisfied dependency expressed through method 'servicesManager' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultServicesManagerExecutionPlanConfigurer' defined in class path resource [org/apereo/cas/config/CasCoreServicesConfiguration$CasCoreServicesManagerExecutionPlanConfiguration.class]: Unsatisfied dependency expressed through method 'defaultServicesManagerExecutionPlanConfigurer' 以上报错怎么处理
03-11
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值