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

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自动注入 测试用例通过。

 

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

 

 

根据提供的引用内容来看,问题是关于"Nacos启动报错: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'user': Unsatisfied dependency expressed through field 'jwtTokenManager'"的。在引用中提到,这个错误是由于在JwtTokenManager类的构造函数中抛出了一个异常,异常信息是"the length of secret key must great than or equal 32 bytes; And the secret key must be encoded by base64"。这意味着密钥的长度必须大于等于32个字节,并且必须使用base64进行编码。 另外,在引用中提到了一个关于数据源配置的问题,其中driverClassName应该设置为"com.mysql.cj.jdbc.Driver"。在引用中提到了在pom文件中引入的mysql-connector-java的版本是5.1.49。 综上所述,解决这个问题的步骤包括: 1. 在JwtTokenManager类的构造函数中使用长度大于等于32个字节的密钥,并确保密钥经过base64编码。 2. 检查数据库配置文件(applicationContext-datasource.xml)中的driverClassName是否正确设置为"com.mysql.cj.jdbc.Driver"。 3. 确保项目中使用的mysql-connector-java的版本与pom文件中引入的版本一致,即5.1.49。 请注意,以上步骤仅为解决给出的错误信息所提供的一般性建议,具体解决方法可能因环境和项目配置而有所不同。如有需要,请查阅相关文档或寻求专业的开发人员支持。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Error creating bean with name ‘user‘: Unsatisfied dependency expressed through field ‘jwt](https://blog.csdn.net/weixin_44012027/article/details/129645876)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Error creating bean with name ‘userService‘: Unsatisfied dependency expressed through field](https://blog.csdn.net/m0_52288433/article/details/127948405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值